update cache add mutex
Showing
1 changed file
with
19 additions
and
23 deletions
| ... | @@ -4,27 +4,14 @@ import ( | ... | @@ -4,27 +4,14 @@ import ( |
| 4 | "errors" | 4 | "errors" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "strconv" | 6 | "strconv" |
| 7 | "sync" | ||
| 7 | "time" | 8 | "time" |
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 10 | const ( | ||
| 11 | Kb = 1024 | ||
| 12 | Mb = 1024 * 1024 | ||
| 13 | Gb = 1024 * 1024 * 1024 | ||
| 14 | ) | ||
| 15 | |||
| 16 | var ( | 11 | var ( |
| 17 | DefaultEvery int = 60 // 1 minute | 12 | DefaultEvery int = 60 // 1 minute |
| 18 | ) | 13 | ) |
| 19 | 14 | ||
| 20 | var ( | ||
| 21 | InvalidCacheItem = errors.New("invalid cache item") | ||
| 22 | ItemIsDirectory = errors.New("can't cache a directory") | ||
| 23 | ItemNotInCache = errors.New("item not in cache") | ||
| 24 | ItemTooLarge = errors.New("item too large for cache") | ||
| 25 | WriteIncomplete = errors.New("incomplete write of cache item") | ||
| 26 | ) | ||
| 27 | |||
| 28 | type BeeItem struct { | 15 | type BeeItem struct { |
| 29 | val interface{} | 16 | val interface{} |
| 30 | Lastaccess time.Time | 17 | Lastaccess time.Time |
| ... | @@ -37,6 +24,7 @@ func (itm *BeeItem) Access() interface{} { | ... | @@ -37,6 +24,7 @@ func (itm *BeeItem) Access() interface{} { |
| 37 | } | 24 | } |
| 38 | 25 | ||
| 39 | type BeeCache struct { | 26 | type BeeCache struct { |
| 27 | lock sync.RWMutex | ||
| 40 | dur time.Duration | 28 | dur time.Duration |
| 41 | items map[string]*BeeItem | 29 | items map[string]*BeeItem |
| 42 | Every int // Run an expiration check Every seconds | 30 | Every int // Run an expiration check Every seconds |
| ... | @@ -44,13 +32,14 @@ type BeeCache struct { | ... | @@ -44,13 +32,14 @@ type BeeCache struct { |
| 44 | 32 | ||
| 45 | // NewDefaultCache returns a new FileCache with sane defaults. | 33 | // NewDefaultCache returns a new FileCache with sane defaults. |
| 46 | func NewBeeCache() *BeeCache { | 34 | func NewBeeCache() *BeeCache { |
| 47 | cache := BeeCache{time.Since(time.Now()), | 35 | cache := BeeCache{dur: time.Since(time.Now()), |
| 48 | nil, | 36 | Every: DefaultEvery} |
| 49 | DefaultEvery} | ||
| 50 | return &cache | 37 | return &cache |
| 51 | } | 38 | } |
| 52 | 39 | ||
| 53 | func (bc *BeeCache) Get(name string) interface{} { | 40 | func (bc *BeeCache) Get(name string) interface{} { |
| 41 | bc.lock.RLock() | ||
| 42 | defer bc.lock.RUnlock() | ||
| 54 | itm, ok := bc.items[name] | 43 | itm, ok := bc.items[name] |
| 55 | if !ok { | 44 | if !ok { |
| 56 | return nil | 45 | return nil |
| ... | @@ -59,8 +48,10 @@ func (bc *BeeCache) Get(name string) interface{} { | ... | @@ -59,8 +48,10 @@ func (bc *BeeCache) Get(name string) interface{} { |
| 59 | } | 48 | } |
| 60 | 49 | ||
| 61 | func (bc *BeeCache) Put(name string, value interface{}, expired int) error { | 50 | func (bc *BeeCache) Put(name string, value interface{}, expired int) error { |
| 51 | bc.lock.Lock() | ||
| 52 | defer bc.lock.Unlock() | ||
| 62 | t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired} | 53 | t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired} |
| 63 | if bc.IsExist(name) { | 54 | if _, ok := bc.items[name]; !ok { |
| 64 | return errors.New("the key is exist") | 55 | return errors.New("the key is exist") |
| 65 | } else { | 56 | } else { |
| 66 | bc.items[name] = &t | 57 | bc.items[name] = &t |
| ... | @@ -69,8 +60,9 @@ func (bc *BeeCache) Put(name string, value interface{}, expired int) error { | ... | @@ -69,8 +60,9 @@ func (bc *BeeCache) Put(name string, value interface{}, expired int) error { |
| 69 | } | 60 | } |
| 70 | 61 | ||
| 71 | func (bc *BeeCache) Delete(name string) (ok bool, err error) { | 62 | func (bc *BeeCache) Delete(name string) (ok bool, err error) { |
| 72 | _, ok = bc.items[name] | 63 | bc.lock.Lock() |
| 73 | if !ok { | 64 | defer bc.lock.Unlock() |
| 65 | if _, ok = bc.items[name]; !ok { | ||
| 74 | return | 66 | return |
| 75 | } | 67 | } |
| 76 | delete(bc.items, name) | 68 | delete(bc.items, name) |
| ... | @@ -82,6 +74,8 @@ func (bc *BeeCache) Delete(name string) (ok bool, err error) { | ... | @@ -82,6 +74,8 @@ func (bc *BeeCache) Delete(name string) (ok bool, err error) { |
| 82 | } | 74 | } |
| 83 | 75 | ||
| 84 | func (bc *BeeCache) IsExist(name string) bool { | 76 | func (bc *BeeCache) IsExist(name string) bool { |
| 77 | bc.lock.RLock() | ||
| 78 | defer bc.lock.RUnlock() | ||
| 85 | _, ok := bc.items[name] | 79 | _, ok := bc.items[name] |
| 86 | return ok | 80 | return ok |
| 87 | } | 81 | } |
| ... | @@ -108,15 +102,15 @@ func (bc *BeeCache) vaccuum() { | ... | @@ -108,15 +102,15 @@ func (bc *BeeCache) vaccuum() { |
| 108 | return | 102 | return |
| 109 | } | 103 | } |
| 110 | for name, _ := range bc.items { | 104 | for name, _ := range bc.items { |
| 111 | if bc.item_expired(name) { | 105 | bc.item_expired(name) |
| 112 | delete(bc.items, name) | ||
| 113 | } | ||
| 114 | } | 106 | } |
| 115 | } | 107 | } |
| 116 | } | 108 | } |
| 117 | 109 | ||
| 118 | // item_expired returns true if an item is expired. | 110 | // item_expired returns true if an item is expired. |
| 119 | func (bc *BeeCache) item_expired(name string) bool { | 111 | func (bc *BeeCache) item_expired(name string) bool { |
| 112 | bc.lock.Lock() | ||
| 113 | defer bc.lock.Unlock() | ||
| 120 | itm, ok := bc.items[name] | 114 | itm, ok := bc.items[name] |
| 121 | if !ok { | 115 | if !ok { |
| 122 | return true | 116 | return true |
| ... | @@ -124,8 +118,10 @@ func (bc *BeeCache) item_expired(name string) bool { | ... | @@ -124,8 +118,10 @@ func (bc *BeeCache) item_expired(name string) bool { |
| 124 | dur := time.Now().Sub(itm.Lastaccess) | 118 | dur := time.Now().Sub(itm.Lastaccess) |
| 125 | sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds())) | 119 | sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds())) |
| 126 | if err != nil { | 120 | if err != nil { |
| 121 | delete(bc.items, name) | ||
| 127 | return true | 122 | return true |
| 128 | } else if sec >= itm.expired { | 123 | } else if sec >= itm.expired { |
| 124 | delete(bc.items, name) | ||
| 129 | return true | 125 | return true |
| 130 | } | 126 | } |
| 131 | return false | 127 | return false | ... | ... |
-
Please register or sign in to post a comment