Skip to content

Commit

Permalink
add travis and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5010 committed Jan 3, 2018
1 parent 7ff2172 commit 1637029
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: go
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
This is a lrucache in the [Go](http:golang.org).

[![Build Status](https://travis-ci.org/0x5010/lrucache.png?branch=master)](https://travis-ci.org/0x5010/lrucache)

Installation
-----------

go get github.com/0x5010/lrucache


Usage
-----------

Get and set with default lrucache:
```go
// cache bytes
lrucache.Set("key", []byte("value"), time.Duration(30*time.Second))

// get
b, ok := lrucache.Get("key")
```
```go
// cache other with gob
type Test struct {
V [][]string
}

var test Test
...
var serialize bytes.Buffer
encoder := gob.NewEncoder(&serialize)
err := encoder.Encode(testStruct)
if err != nil {
return nil, err
}
lrucache.Set("key", serialize.Bytes(), time.Duration(30*time.Second))

// get
b, ok := lrucache.Get("key")

t := &Test{}
decoder := gob.NewDecoder(bytes.NewReader(b))
err := decoder.Decode(t)
```
new cache:
```go
myCache := lrucache.New(time.Duration(20 * time.Minute))

myCache.Set("key", []byte("value"), time.Duration(30*time.Second))

b, ok := myCache.Get("key")

```

41 changes: 28 additions & 13 deletions lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ func (i *item) expired() bool {
return value
}

type coverageCache struct {
// CoverageCache 缓存
type CoverageCache struct {
mutex sync.RWMutex
items map[string]*item
}

func (cache *coverageCache) set(key string, data []byte, ttl time.Duration) {
// Set 缓存数据
func (cache *CoverageCache) Set(key string, data []byte, ttl time.Duration) {
cache.mutex.Lock()
defer cache.mutex.Unlock()
item := &item{
Expand All @@ -47,7 +49,8 @@ func (cache *coverageCache) set(key string, data []byte, ttl time.Duration) {
cache.items[key] = item
}

func (cache *coverageCache) get(key string) (data []byte, found bool) {
// Get 获取缓存
func (cache *CoverageCache) Get(key string) (data []byte, found bool) {
cache.mutex.RLock()
defer cache.mutex.RUnlock()
item, exists := cache.items[key]
Expand All @@ -61,14 +64,15 @@ func (cache *coverageCache) get(key string) (data []byte, found bool) {
return
}

func (cache *coverageCache) count() int {
// Count 获取缓存个数
func (cache *CoverageCache) Count() int {
cache.mutex.RLock()
defer cache.mutex.RUnlock()
count := len(cache.items)
return count
}

func (cache *coverageCache) cleanup() {
func (cache *CoverageCache) cleanup() {
cache.mutex.Lock()
for key, item := range cache.items {
if item.expired() {
Expand All @@ -78,7 +82,7 @@ func (cache *coverageCache) cleanup() {
cache.mutex.Unlock()
}

func (cache *coverageCache) clean(duration time.Duration) {
func (cache *CoverageCache) clean(duration time.Duration) {
cleanTicker := time.NewTicker(duration)
defer cleanTicker.Stop()

Expand All @@ -90,22 +94,33 @@ func (cache *coverageCache) clean(duration time.Duration) {
}
}

// New 新建缓存
func New(duration time.Duration) *CoverageCache {
cache = &CoverageCache{
items: map[string]*item{},
}
go cache.clean(duration)
return cache
}

// Cache 默认全局缓存
var cache *coverageCache
var cache *CoverageCache

// Get 从默认缓存中读取
func Get(key string) ([]byte, bool) {
return cache.get(key)
return cache.Get(key)
}

// Set 设置缓存到默认缓存中
func Set(key string, value []byte, ttl time.Duration) {
cache.set(key, value, ttl)
cache.Set(key, value, ttl)
}

// Count 当前缓存个数
func Count() int {
return cache.Count()
}

func init() {
cache = &coverageCache{
items: map[string]*item{},
}
go cache.clean(time.Duration(20 * time.Minute))
cache = New(time.Duration(20 * time.Minute))
}

0 comments on commit 1637029

Please sign in to comment.