Skip to content

Commit

Permalink
feat: 实现缓存项目
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed May 29, 2023
1 parent 887036a commit a7bafa0
Show file tree
Hide file tree
Showing 7 changed files with 677 additions and 1 deletion.
148 changes: 147 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,147 @@
# cache-demo
## cache-demo
## 基于golang实现的本地缓存

### 项目思路与功能
使用map实现本地缓存
1. 实现LRU缓存
2. 实现TTL过期时间LRU缓存
3. 实现TTL过期时间缓存

### 使用示例:
```go
func TestLRUCache(t *testing.T) {

config := cache.NewCacheConfig(0, 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

lruCache := cache.NewCache(config.LRUCacheMode(), config)

lruCache.Add(1, "this is test 1")
lruCache.Add(2, "this is test 2")
lruCache.Add(3, "this is test 3")
lruCache.Add(4, "this is test 4")
lruCache.Add(5, "this is test 5")
lruCache.Add(6, "this is test 6")

log.Println("init LRU lruCache finish")
log.Printf("lruCache size: %d", lruCache.Size())

if v, ok := lruCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
}

if v, ok := lruCache.Get(3); ok {
log.Printf("get key (%d) success, value: %s", 3, v)
}

lruCache.Add(7, "this is test 7")
lruCache.Add(8, "this is test 8")

if _, ok := lruCache.Get(4); !ok {
log.Printf("get key (%d) failed", 4)
}
}

func TestLRUWithTTLCache(t *testing.T) {
config := cache.NewCacheConfig(4, 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

lruWithTTLCache := cache.NewCache(config.LRUWithTTLCacheMode(), config)

lruWithTTLCache.Add(1, "this is test 1")
lruWithTTLCache.Add(2, "this is test 2")
lruWithTTLCache.Add(3, "this is test 3")
lruWithTTLCache.Add(4, "this is test 4")
lruWithTTLCache.Add(5, "this is test 5")
lruWithTTLCache.Add(6, "this is test 6")

log.Println("init LRU TTL cache finish")
log.Printf("cache size: %d", lruWithTTLCache.Size())

if v, ok := lruWithTTLCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
}

time.Sleep(5 * time.Second)

if _, ok := lruWithTTLCache.Get(2); !ok {
log.Printf("get key (%d) failed", 2)
}
}

func TestTTLCache(t *testing.T) {
config := cache.NewCacheConfig(time.Duration(10), 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

tllCache := cache.NewCache(config.TTLCacheMode(true), config)

tllCache.Add(1, "this is test 1")
time.Sleep(3 * time.Millisecond)
tllCache.Add(2, "this is test 2")
time.Sleep(3 * time.Millisecond)
tllCache.Add(3, "this is test 3")
time.Sleep(3 * time.Millisecond)
tllCache.Add(4, "this is test 4")
time.Sleep(3 * time.Millisecond)
tllCache.Add(5, "this is test 5")
time.Sleep(3 * time.Millisecond)
tllCache.Add(6, "this is test 6")
time.Sleep(3 * time.Millisecond)

log.Println("init TTL cache finish")
log.Printf("cache size: %d", tllCache.Size())

time.Sleep(3 * time.Second)

if v, ok := tllCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
log.Printf("cache size: %d", tllCache.Size())
}

time.Sleep(3 * time.Second)

if _, ok := tllCache.Get(3); !ok {
log.Printf("get key (%d) failed", 3)
log.Printf("cache size: %d", tllCache.Size())
}

log.Println("Add new three item")
tllCache.Add(7, "this is test 7")
tllCache.Add(8, "this is test 8")
tllCache.Add(9, "this is test 9")
log.Printf("cache size: %d", tllCache.Size())

if v, ok := tllCache.Get(8); ok {
log.Printf("get key (%d) success, value: %s", 8, v)
}

}
```

145 changes: 145 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package test

import (
"fmt"
"github.com/cache-example/pkg/cache"
"log"
"testing"
"time"
)

func TestLRUCache(t *testing.T) {

config := cache.NewCacheConfig(0, 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

lruCache := cache.NewCache(config.LRUCacheMode(), config)

lruCache.Add(1, "this is test 1")
lruCache.Add(2, "this is test 2")
lruCache.Add(3, "this is test 3")
lruCache.Add(4, "this is test 4")
lruCache.Add(5, "this is test 5")
lruCache.Add(6, "this is test 6")

log.Println("init LRU lruCache finish")
log.Printf("lruCache size: %d", lruCache.Size())

if v, ok := lruCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
}

if v, ok := lruCache.Get(3); ok {
log.Printf("get key (%d) success, value: %s", 3, v)
}

lruCache.Add(7, "this is test 7")
lruCache.Add(8, "this is test 8")

if _, ok := lruCache.Get(4); !ok {
log.Printf("get key (%d) failed", 4)
}
}

func TestLRUWithTTLCache(t *testing.T) {
config := cache.NewCacheConfig(4, 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

lruWithTTLCache := cache.NewCache(config.LRUWithTTLCacheMode(), config)

lruWithTTLCache.Add(1, "this is test 1")
lruWithTTLCache.Add(2, "this is test 2")
lruWithTTLCache.Add(3, "this is test 3")
lruWithTTLCache.Add(4, "this is test 4")
lruWithTTLCache.Add(5, "this is test 5")
lruWithTTLCache.Add(6, "this is test 6")

log.Println("init LRU TTL cache finish")
log.Printf("cache size: %d", lruWithTTLCache.Size())

if v, ok := lruWithTTLCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
}

time.Sleep(5 * time.Second)

if _, ok := lruWithTTLCache.Get(2); !ok {
log.Printf("get key (%d) failed", 2)
}
}

func TestTTLCache(t *testing.T) {
config := cache.NewCacheConfig(time.Duration(10), 20, cache.ChangeCallbacks{
OnAdd: func() {
fmt.Println("entry add...")
},
OnGet: func() {
fmt.Println("entry get...")
},
OnRemove: func() {
fmt.Println("entry delete...")
},
})

tllCache := cache.NewCache(config.TTLCacheMode(true), config)

tllCache.Add(1, "this is test 1")
time.Sleep(3 * time.Millisecond)
tllCache.Add(2, "this is test 2")
time.Sleep(3 * time.Millisecond)
tllCache.Add(3, "this is test 3")
time.Sleep(3 * time.Millisecond)
tllCache.Add(4, "this is test 4")
time.Sleep(3 * time.Millisecond)
tllCache.Add(5, "this is test 5")
time.Sleep(3 * time.Millisecond)
tllCache.Add(6, "this is test 6")
time.Sleep(3 * time.Millisecond)

log.Println("init TTL cache finish")
log.Printf("cache size: %d", tllCache.Size())

time.Sleep(3 * time.Second)

if v, ok := tllCache.Get(2); ok {
log.Printf("get key (%d) success, value: %s", 2, v)
log.Printf("cache size: %d", tllCache.Size())
}

time.Sleep(3 * time.Second)

if _, ok := tllCache.Get(3); !ok {
log.Printf("get key (%d) failed", 3)
log.Printf("cache size: %d", tllCache.Size())
}

log.Println("Add new three item")
tllCache.Add(7, "this is test 7")
tllCache.Add(8, "this is test 8")
tllCache.Add(9, "this is test 9")
log.Printf("cache size: %d", tllCache.Size())

if v, ok := tllCache.Get(8); ok {
log.Printf("get key (%d) success, value: %s", 8, v)
}

}

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cache-example

go 1.18
Loading

0 comments on commit a7bafa0

Please sign in to comment.