Skip to content

Commit

Permalink
Feat: ttl in memory-cache / Add: webdav from standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Dec 16, 2019
1 parent e6d2a94 commit 327a3c1
Show file tree
Hide file tree
Showing 15 changed files with 7,372 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/rafaeljusto/redigomock v0.0.0-20191117212112-00b2509252a1
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
gopkg.in/go-playground/validator.v8 v8.18.2
gopkg.in/ini.v1 v1.51.0 // indirect
)
45 changes: 42 additions & 3 deletions pkg/cache/memo.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
package cache

import "sync"
import (
"sync"
"time"
)

// MemoStore 内存存储驱动
type MemoStore struct {
Store *sync.Map
}

// item 存储的对象
type itemWithTTL struct {
expires int64
value interface{}
}

func newItem(value interface{}, expires int) itemWithTTL {
expires64 := int64(expires)
if expires > 0 {
expires64 = time.Now().Unix() + expires64
}
return itemWithTTL{
value: value,
expires: expires64,
}
}

// getValue 从itemWithTTL中取值
func getValue(item interface{}, ok bool) (interface{}, bool) {
if !ok {
return nil, ok
}

var itemObj itemWithTTL
if itemObj, ok = item.(itemWithTTL); !ok {
return nil, false
}

if itemObj.expires > 0 && itemObj.expires < time.Now().Unix() {
return nil, false
}

return itemObj.value, ok

}

// NewMemoStore 新建内存存储
func NewMemoStore() *MemoStore {
return &MemoStore{
Expand All @@ -16,13 +55,13 @@ func NewMemoStore() *MemoStore {

// Set 存储值
func (store *MemoStore) Set(key string, value interface{}, ttl int) error {
store.Store.Store(key, value)
store.Store.Store(key, newItem(value, ttl))
return nil
}

// Get 取值
func (store *MemoStore) Get(key string) (interface{}, bool) {
return store.Store.Load(key)
return getValue(store.Store.Load(key))
}

// Gets 批量取值
Expand Down
12 changes: 11 additions & 1 deletion pkg/cache/memo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestNewMemoStore(t *testing.T) {
Expand All @@ -22,7 +23,7 @@ func TestMemoStore_Set(t *testing.T) {

val, ok := store.Store.Load("KEY")
asserts.True(ok)
asserts.Equal("vAL", val)
asserts.Equal("vAL", val.(itemWithTTL).value)
}

func TestMemoStore_Get(t *testing.T) {
Expand Down Expand Up @@ -58,6 +59,15 @@ func TestMemoStore_Get(t *testing.T) {
asserts.Equal(test, res)
}

// 过期
{
_ = store.Set("string", "string_val", 1)
time.Sleep(time.Duration(2) * time.Second)
val, ok := store.Get("string")
asserts.Nil(val)
asserts.False(ok)
}

}

func TestMemoStore_Gets(t *testing.T) {
Expand Down
Loading

0 comments on commit 327a3c1

Please sign in to comment.