Skip to content

Commit 305df0b

Browse files
committed
Adjust directory structure
1 parent ebec94b commit 305df0b

File tree

7 files changed

+65
-71
lines changed

7 files changed

+65
-71
lines changed

cache/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func main() {
2020
var foo string
2121

2222
// Create a cache with memory store
23-
cache, _ := cache.NewCache(memory.NewStore("thinkgo"))
23+
c, _ := cache.NewCache(cache.NewMemoryStore("thinkgo"))
2424

2525
// Set the value
26-
cache.Put("foo", "thinkgo", 10 * time.Minute)
26+
c.Put("foo", "thinkgo", 10 * time.Minute)
2727

2828
// Get the string associated with the key "foo" from the cache
29-
cache.Get("foo", &foo)
29+
c.Get("foo", &foo)
3030
}
3131
```
3232

cache/memory/memory_store.go renamed to cache/memory_store.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package memory
1+
package cache
22

33
import (
44
"errors"
@@ -8,37 +8,37 @@ import (
88
"time"
99
)
1010

11-
type Item struct {
11+
type item struct {
1212
Object interface{}
1313
Expiration int64
1414
}
1515

1616
// Expired Returns true if the item has expired.
17-
func (item Item) Expired() bool {
17+
func (item item) Expired() bool {
1818
if item.Expiration == 0 {
1919
return false
2020
}
2121
return time.Now().UnixNano() > item.Expiration
2222
}
2323

24-
type Store struct {
24+
type MemoryStore struct {
2525
prefix string
26-
items map[string]Item
26+
items map[string]item
2727
mu sync.RWMutex
2828
cleanupTimer *time.Timer
2929
}
3030

3131
// NewStore Create a memory cache store
32-
func NewStore(prefix string) *Store {
33-
s := &Store{
34-
items: make(map[string]Item),
32+
func NewMemoryStore(prefix string) *MemoryStore {
33+
s := &MemoryStore{
34+
items: make(map[string]item),
3535
}
3636
return s.SetPrefix(prefix)
3737
}
3838

3939
// Get get cached value by key.
4040
// func (s *Store) Get(key string) (interface{}, error) {
41-
func (s *Store) Get(key string, val interface{}) error {
41+
func (s *MemoryStore) Get(key string, val interface{}) error {
4242
s.mu.RLock()
4343
defer s.mu.RUnlock()
4444

@@ -64,7 +64,7 @@ func (s *Store) Get(key string, val interface{}) error {
6464
}
6565

6666
// Put set cached value with key and expire time.
67-
func (s *Store) Put(key string, val interface{}, timeout time.Duration) error {
67+
func (s *MemoryStore) Put(key string, val interface{}, timeout time.Duration) error {
6868
var e int64
6969
if timeout > 0 {
7070
e = time.Now().Add(timeout).UnixNano()
@@ -73,7 +73,7 @@ func (s *Store) Put(key string, val interface{}, timeout time.Duration) error {
7373
s.mu.RLock()
7474
defer s.mu.RUnlock()
7575

76-
s.items[s.prefix+key] = Item{
76+
s.items[s.prefix+key] = item{
7777
Object: val,
7878
Expiration: e,
7979
}
@@ -86,7 +86,7 @@ func (s *Store) Put(key string, val interface{}, timeout time.Duration) error {
8686
}
8787

8888
// Exist check cache's existence in memory.
89-
func (s *Store) Exist(key string) bool {
89+
func (s *MemoryStore) Exist(key string) bool {
9090
s.mu.RLock()
9191
defer s.mu.RUnlock()
9292

@@ -100,28 +100,28 @@ func (s *Store) Exist(key string) bool {
100100
}
101101

102102
// Forget Remove an item from the cache.
103-
func (s *Store) Forget(key string) error {
103+
func (s *MemoryStore) Forget(key string) error {
104104
delete(s.items, s.prefix+key)
105105
return nil
106106
}
107107

108108
// Remove all items from the cache.
109-
func (s *Store) Flush() error {
109+
func (s *MemoryStore) Flush() error {
110110
s.mu.RLock()
111111
defer s.mu.RUnlock()
112112

113-
s.items = map[string]Item{}
113+
s.items = map[string]item{}
114114

115115
return nil
116116
}
117117

118118
// GetPrefix Get the cache key prefix.
119-
func (s *Store) GetPrefix() string {
119+
func (s *MemoryStore) GetPrefix() string {
120120
return s.prefix
121121
}
122122

123123
// SetPrefix Set the cache key prefix.
124-
func (s *Store) SetPrefix(prefix string) *Store {
124+
func (s *MemoryStore) SetPrefix(prefix string) *MemoryStore {
125125
if len(prefix) != 0 {
126126
s.prefix = fmt.Sprintf("%s:", prefix)
127127
} else {
@@ -131,7 +131,7 @@ func (s *Store) SetPrefix(prefix string) *Store {
131131
}
132132

133133
// Delete all expired items from the cache.
134-
func (s *Store) DeleteExpired() {
134+
func (s *MemoryStore) DeleteExpired() {
135135
s.mu.RLock()
136136
defer s.mu.RUnlock()
137137

cache/memory/memory_store_test.go renamed to cache/memory_store_test.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package memory
1+
package cache
22

33
import (
44
"errors"
@@ -11,13 +11,12 @@ type CacheUser struct {
1111
Age int
1212
}
1313

14-
15-
func getStore() *Store {
16-
return NewStore("cache")
14+
func getMemoryStore() *MemoryStore {
15+
return NewMemoryStore("cache")
1716
}
1817

19-
func TestStore(t *testing.T) {
20-
s := getStore()
18+
func TestMemoryStore(t *testing.T) {
19+
s := getMemoryStore()
2120
var a int
2221
var b string
2322
var c CacheUser
@@ -28,7 +27,7 @@ func TestStore(t *testing.T) {
2827
}
2928

3029
err = s.Get("b", &b)
31-
if err == nil{
30+
if err == nil {
3231
t.Error("Getting B found value that shouldn't exist:", b)
3332
}
3433

@@ -40,7 +39,7 @@ func TestStore(t *testing.T) {
4039
t.Error(err)
4140
}
4241

43-
if a != 1{
42+
if a != 1 {
4443
t.Error("Expect: ", 1)
4544
}
4645

@@ -49,7 +48,7 @@ func TestStore(t *testing.T) {
4948
t.Error(err)
5049
}
5150

52-
if b != "thinkgo"{
51+
if b != "thinkgo" {
5352
t.Error("Expect: ", "thinkgo")
5453
}
5554

@@ -72,8 +71,8 @@ func TestStore(t *testing.T) {
7271
t.Logf("user:name=%s,age=%d", c.Name, c.Age)
7372
}
7473

75-
func TestStoreDuration(t *testing.T) {
76-
s := getStore()
74+
func TestMemoryStoreDuration(t *testing.T) {
75+
s := getMemoryStore()
7776
var a int
7877

7978
s.Put("a", 3, 20*time.Millisecond)
@@ -85,9 +84,8 @@ func TestStoreDuration(t *testing.T) {
8584
}
8685
}
8786

88-
89-
func TestStoreForgetAndExist(t *testing.T) {
90-
s := getStore()
87+
func TestMemoryStoreForgetAndExist(t *testing.T) {
88+
s := getMemoryStore()
9189
err := s.Put("forget", "Forget me", 10*time.Minute)
9290
if err != nil {
9391
t.Error(err)
@@ -109,8 +107,8 @@ func TestStoreForgetAndExist(t *testing.T) {
109107
}
110108
}
111109

112-
func TestStoreFlush(t *testing.T) {
113-
s := getStore()
110+
func TestMemoryStoreFlush(t *testing.T) {
111+
s := getMemoryStore()
114112
err := s.Put("Flush", "Flush all", 10*time.Minute)
115113
if err != nil {
116114
t.Error(err)
@@ -130,4 +128,4 @@ func TestStoreFlush(t *testing.T) {
130128
if exist == true {
131129
t.Error(errors.New("Expect false"))
132130
}
133-
}
131+
}

cache/redis/redis_store.go renamed to cache/redis_store.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redis
1+
package cache
22

33
import (
44
"encoding/json"
@@ -8,19 +8,19 @@ import (
88
"github.com/gomodule/redigo/redis"
99
)
1010

11-
type Store struct {
11+
type RedisStore struct {
1212
pool *redis.Pool // redis connection pool
1313
prefix string
1414
}
1515

1616
// NewStore Create a redis cache store
17-
func NewStore(pool *redis.Pool, prefix string) *Store {
18-
s := Store{}
17+
func NewRedisStore(pool *redis.Pool, prefix string) *RedisStore {
18+
s := RedisStore{}
1919
return s.SetPool(pool).SetPrefix(prefix)
2020
}
2121

2222
// Get get cached value by key.
23-
func (s *Store) Get(key string, val interface{}) error {
23+
func (s *RedisStore) Get(key string, val interface{}) error {
2424
c := s.pool.Get()
2525
defer c.Close()
2626

@@ -33,7 +33,7 @@ func (s *Store) Get(key string, val interface{}) error {
3333
}
3434

3535
// Put set cached value with key and expire time.
36-
func (s *Store) Put(key string, val interface{}, timeout time.Duration) error {
36+
func (s *RedisStore) Put(key string, val interface{}, timeout time.Duration) error {
3737
b, err := json.Marshal(val)
3838
if err != nil {
3939
return err
@@ -45,7 +45,7 @@ func (s *Store) Put(key string, val interface{}, timeout time.Duration) error {
4545
}
4646

4747
// Exist check cache's existence in redis.
48-
func (s *Store) Exist(key string) bool {
48+
func (s *RedisStore) Exist(key string) bool {
4949
c := s.pool.Get()
5050
defer c.Close()
5151
v, err := redis.Bool(c.Do("EXISTS", s.prefix+key))
@@ -56,15 +56,15 @@ func (s *Store) Exist(key string) bool {
5656
}
5757

5858
// Forget Remove an item from the cache.
59-
func (s *Store) Forget(key string) error {
59+
func (s *RedisStore) Forget(key string) error {
6060
c := s.pool.Get()
6161
defer c.Close()
6262
_, err := c.Do("DEL", s.prefix+key)
6363
return err
6464
}
6565

6666
// Remove all items from the cache.
67-
func (s *Store) Flush() error {
67+
func (s *RedisStore) Flush() error {
6868
c := s.pool.Get()
6969
defer c.Close()
7070
keys, err := redis.Strings(c.Do("KEYS", s.prefix+"*"))
@@ -80,18 +80,18 @@ func (s *Store) Flush() error {
8080
}
8181

8282
// SetPool Get the redis pool.
83-
func (s *Store) SetPool(pool *redis.Pool) *Store {
83+
func (s *RedisStore) SetPool(pool *redis.Pool) *RedisStore {
8484
s.pool = pool
8585
return s
8686
}
8787

8888
// GetPrefix Get the cache key prefix.
89-
func (s *Store) GetPrefix() string {
89+
func (s *RedisStore) GetPrefix() string {
9090
return s.prefix
9191
}
9292

9393
// SetPrefix Set the cache key prefix.
94-
func (s *Store) SetPrefix(prefix string) *Store {
94+
func (s *RedisStore) SetPrefix(prefix string) *RedisStore {
9595
if len(prefix) != 0 {
9696
s.prefix = fmt.Sprintf("%s:", prefix)
9797
} else {

cache/redis/redis_store_test.go renamed to cache/redis_store_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redis
1+
package cache
22

33
import (
44
"errors"
@@ -8,11 +8,6 @@ import (
88
"github.com/gomodule/redigo/redis"
99
)
1010

11-
type CacheUser struct {
12-
Name string
13-
Age int
14-
}
15-
1611
func GetPool() *redis.Pool {
1712
return &redis.Pool{
1813
MaxIdle: 5,
@@ -30,13 +25,13 @@ func GetPool() *redis.Pool {
3025
}
3126
}
3227

33-
func getStore() *Store {
28+
func getRedisStore() *RedisStore {
3429
pool := GetPool()
35-
return NewStore(pool, "cache")
30+
return NewRedisStore(pool, "cache")
3631
}
3732

38-
func TestStoreInt(t *testing.T) {
39-
s := getStore()
33+
func TestRedisStoreInt(t *testing.T) {
34+
s := getRedisStore()
4035
err := s.Put("int", 9811, 10*time.Minute)
4136
if err != nil {
4237
t.Error(err)
@@ -52,8 +47,8 @@ func TestStoreInt(t *testing.T) {
5247
t.Logf("int:%d", v)
5348
}
5449

55-
func TestStoreString(t *testing.T) {
56-
s := getStore()
50+
func TestRedisStoreString(t *testing.T) {
51+
s := getRedisStore()
5752
err := s.Put("str", "this is a string", 10*time.Minute)
5853
if err != nil {
5954
t.Error(err)
@@ -70,7 +65,7 @@ func TestStoreString(t *testing.T) {
7065
}
7166

7267
func TestStoreStruct(t *testing.T) {
73-
s := getStore()
68+
s := getRedisStore()
7469
err := s.Put(
7570
"user", CacheUser{
7671
Name: "alice",
@@ -92,8 +87,8 @@ func TestStoreStruct(t *testing.T) {
9287
t.Logf("user:name=%s,age=%d", user.Name, user.Age)
9388
}
9489

95-
func TestStoreForgetAndExist(t *testing.T) {
96-
s := getStore()
90+
func TestRedisStoreForgetAndExist(t *testing.T) {
91+
s := getRedisStore()
9792
err := s.Put("forget", "Forget me", 10*time.Minute)
9893
if err != nil {
9994
t.Error(err)
@@ -115,8 +110,8 @@ func TestStoreForgetAndExist(t *testing.T) {
115110
}
116111
}
117112

118-
func TestStoreFlush(t *testing.T) {
119-
s := getStore()
113+
func TestRedisStoreFlush(t *testing.T) {
114+
s := getRedisStore()
120115
err := s.Put("Flush", "Flush all", 10*time.Minute)
121116
if err != nil {
122117
t.Error(err)

0 commit comments

Comments
 (0)