Skip to content

Commit

Permalink
增加runtime cache用例
Browse files Browse the repository at this point in the history
修改并发问题
  • Loading branch information
zouyx committed Jul 17, 2017
1 parent fbb4372 commit 7a9c753
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
23 changes: 13 additions & 10 deletions cache/runtime/cache_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func (ca *RuntimeCache) GetInt64(key string) (int64, error) {
func (ca *RuntimeCache) Set(key string, value interface{}, ttl int64) error {
ca.Lock()
defer ca.Unlock()
ca.initValue(key,value,ttl)
return nil
}

func (ca *RuntimeCache) initValue(key string, value interface{}, ttl int64) error {
ca.items[key] = &RuntimeItem{
value: value,
createTime: time.Now(),
Expand All @@ -117,16 +122,13 @@ func (ca *RuntimeCache) Set(key string, value interface{}, ttl int64) error {

// Incr increase int64 counter in runtime cache.
func (ca *RuntimeCache) Incr(key string) (int64, error) {
ca.RLock()
ca.Lock()
item, ok := ca.items[key]
ca.RUnlock()
if !ok {
//if not exists, auto set new with 0
ca.Set(key, ZeroInt64, 0)
ca.initValue(key,ZeroInt64,0)
//reload
ca.RLock()
item, _ = ca.items[key]
ca.RUnlock()
}

switch item.value.(type) {
Expand All @@ -146,22 +148,21 @@ func (ca *RuntimeCache) Incr(key string) (int64, error) {
return 0, errors.New("item val is not (u)int (u)int32 (u)int64")
}

ca.Unlock()

val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64)
return val, nil
}

// Decr decrease counter in runtime cache.
func (ca *RuntimeCache) Decr(key string) (int64, error) {
ca.RLock()
ca.Lock()
item, ok := ca.items[key]
ca.RUnlock()
if !ok {
//if not exists, auto set new with 0
ca.Set(key, ZeroInt64, 0)
ca.initValue(key,ZeroInt64,0)
//reload
ca.RLock()
item, _ = ca.items[key]
ca.RUnlock()
}
switch item.value.(type) {
case int:
Expand Down Expand Up @@ -191,6 +192,8 @@ func (ca *RuntimeCache) Decr(key string) (int64, error) {
default:
return 0, errors.New("item val is not int int64 int32")
}
ca.Unlock()

val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64)
return val, nil
}
Expand Down
86 changes: 86 additions & 0 deletions cache/runtime/cache_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"
"github.com/devfeel/dotweb/test"
"sync"
)

const (
Expand Down Expand Up @@ -77,6 +78,91 @@ func testRuntimeCache(t *testing.T,insertValue interface{},f func(cache *Runtime
time.Sleep(5*time.Second)
}

func TestRuntimeCache_Delete(t *testing.T) {
cache:=NewTestRuntimeCache()
cache.Set(TEST_CACHE_KEY,TEST_CACHE_VALUE,5)

value,e:=cache.Get(TEST_CACHE_KEY)

test.Nil(t,e)
test.Equal(t,TEST_CACHE_VALUE,value)

cache.Delete(TEST_CACHE_KEY)

value,e=cache.Get(TEST_CACHE_KEY)
test.Nil(t,e)
test.Nil(t,value)
}

func TestRuntimeCache_ClearAll(t *testing.T) {
cache:=NewTestRuntimeCache()
cache.Set(TEST_CACHE_KEY,TEST_CACHE_VALUE,5)
cache.Set("2",TEST_CACHE_VALUE,5)
cache.Set("3",TEST_CACHE_VALUE,5)

test.Equal(t,3,len(cache.items))

cache.ClearAll()

test.Equal(t,0,len(cache.items))
}

func TestRuntimeCache_Incr(t *testing.T) {
cache:=NewTestRuntimeCache()
var wg sync.WaitGroup
wg.Add(2)

go func(cache *RuntimeCache) {
for i := 0; i < 50; i++ {
cache.Incr(TEST_CACHE_KEY)
}

wg.Add(-1)
}(cache)

go func(cache *RuntimeCache) {
for i := 0; i < 50; i++ {
cache.Incr(TEST_CACHE_KEY)
}
wg.Add(-1)
}(cache)

wg.Wait()

value,e:=cache.GetInt(TEST_CACHE_KEY)
test.Nil(t,e)

test.Equal(t,100,value)
}

func TestRuntimeCache_Decr(t *testing.T) {
cache:=NewTestRuntimeCache()
var wg sync.WaitGroup
wg.Add(2)

go func(cache *RuntimeCache) {
for i := 0; i < 50; i++ {
cache.Decr(TEST_CACHE_KEY)
}

wg.Add(-1)
}(cache)

go func(cache *RuntimeCache) {
for i := 0; i < 50; i++ {
cache.Decr(TEST_CACHE_KEY)
}
wg.Add(-1)
}(cache)

wg.Wait()

value,e:=cache.GetInt(TEST_CACHE_KEY)
test.Nil(t,e)

test.Equal(t,-100,value)
}

func NewTestRuntimeCache() *RuntimeCache {
cache := RuntimeCache{items: make(map[string]*RuntimeItem), gcInterval: DefaultTestGCInterval}
go cache.gc()
Expand Down

0 comments on commit 7a9c753

Please sign in to comment.