Skip to content

Commit e15abac

Browse files
author
周倩
committed
Release 0.0.3
1 parent 7650f22 commit e15abac

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

sensors_abtesting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
SDK_VERSION = "0.0.2"
11+
SDK_VERSION = "0.0.3"
1212
LIB_NAME = "Golang"
1313
)
1414

utils/lru/lru.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ limitations under the License.
1616

1717
package lru
1818

19-
import "container/list"
19+
import (
20+
"container/list"
21+
"sync"
22+
)
2023

21-
// Cache is an LRU cache. It is not safe for concurrent access.
2224
type Cache struct {
2325
// MaxEntries is the maximum number of cache entries before
2426
// an item is evicted. Zero means no limit.
@@ -30,6 +32,7 @@ type Cache struct {
3032

3133
ll *list.List
3234
cache map[interface{}]*list.Element
35+
lock sync.RWMutex
3336
}
3437

3538
// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
@@ -53,6 +56,8 @@ func New(maxEntries int) *Cache {
5356

5457
// Add adds a value to the cache.
5558
func (c *Cache) Add(key Key, value interface{}) {
59+
c.lock.Lock()
60+
defer c.lock.Unlock()
5661
if c.cache == nil {
5762
c.cache = make(map[interface{}]*list.Element)
5863
c.ll = list.New()
@@ -65,12 +70,14 @@ func (c *Cache) Add(key Key, value interface{}) {
6570
ele := c.ll.PushFront(&entry{key, value})
6671
c.cache[key] = ele
6772
if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
68-
c.RemoveOldest()
73+
c.removeOldest()
6974
}
7075
}
7176

7277
// Get looks up a key's value from the cache.
7378
func (c *Cache) Get(key Key) (value interface{}, ok bool) {
79+
c.lock.Lock()
80+
defer c.lock.Unlock()
7481
if c.cache == nil {
7582
return
7683
}
@@ -83,6 +90,8 @@ func (c *Cache) Get(key Key) (value interface{}, ok bool) {
8390

8491
// Remove removes the provided key from the cache.
8592
func (c *Cache) Remove(key Key) {
93+
c.lock.Lock()
94+
defer c.lock.Unlock()
8695
if c.cache == nil {
8796
return
8897
}
@@ -92,7 +101,7 @@ func (c *Cache) Remove(key Key) {
92101
}
93102

94103
// RemoveOldest removes the oldest item from the cache.
95-
func (c *Cache) RemoveOldest() {
104+
func (c *Cache) removeOldest() {
96105
if c.cache == nil {
97106
return
98107
}
@@ -113,6 +122,8 @@ func (c *Cache) removeElement(e *list.Element) {
113122

114123
// Len returns the number of items in the cache.
115124
func (c *Cache) Len() int {
125+
c.lock.Lock()
126+
defer c.lock.Unlock()
116127
if c.cache == nil {
117128
return 0
118129
}
@@ -121,6 +132,8 @@ func (c *Cache) Len() int {
121132

122133
// Clear purges all stored items from the cache.
123134
func (c *Cache) Clear() {
135+
c.lock.Lock()
136+
defer c.lock.Unlock()
124137
if c.OnEvicted != nil {
125138
for _, e := range c.cache {
126139
kv := e.Value.(*entry)

0 commit comments

Comments
 (0)