mirror of https://github.com/rjbasitali/go-cache
Compare commits
10 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
7431514531 | |
|
|
d18573b146 | |
|
|
d2761754a0 | |
|
|
f68be2a82a | |
|
|
3e79112696 | |
|
|
9b5a155a44 | |
|
|
d0a7fc26e1 | |
|
|
9242ee35fe | |
|
|
8142b23bcb | |
|
|
de2865c97f |
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Basit Ali
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
# go-cache
|
||||||
|
|
||||||
|
Simple thread safe key/value cache for Go.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
You can initialize the cache using:
|
||||||
|
|
||||||
|
```go
|
||||||
|
cache = gocache.NewCache()
|
||||||
|
```
|
||||||
|
|
||||||
|
And perform the operations using:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// GET a value using key, which returns (value, error)
|
||||||
|
cache.Get(key)
|
||||||
|
|
||||||
|
// SET a value using key
|
||||||
|
cache.Set(key, value)
|
||||||
|
|
||||||
|
// REMOVE a key/value pair using key
|
||||||
|
cache.Remove(key)
|
||||||
|
|
||||||
|
// And FLUSH, which removes all items from the cache
|
||||||
|
cache.Flush()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Cache Expiration
|
||||||
|
|
||||||
|
You can also add an **expiration** to the cache, expired cache will be auto deleted by the `sweeper`.
|
||||||
|
|
||||||
|
To initialize a cache using `sweeper` we can use:
|
||||||
|
|
||||||
|
```go
|
||||||
|
cache := NewCacheWithSweeper(10 * time.Minute, 10 * time.Minute, decisionFunc, onEviction)
|
||||||
|
```
|
||||||
|
|
||||||
|
Here, first two parameters are `interval` and `expireAfter`, sweeper will run in the background after specified `interval` to check for expired cache to delete.
|
||||||
|
|
||||||
|
The `decisionFunc` and `onEviction` are **optional** functions with the following signature:
|
||||||
|
```go
|
||||||
|
decisionFunc func(v interface{}) bool
|
||||||
|
|
||||||
|
onEviction func(k string, v interface{})
|
||||||
|
```
|
||||||
|
|
||||||
|
We can use `decisionFunc` as a callback, it will be called by the cache before deleting a key/value, it takes in the value to delete and returns `true` to delete and `false` for not deleting a key/value pair even after expired.
|
||||||
|
|
||||||
|
The `onEviction` is a callback which will be called every time a cache is deleted with its key/value as parameters.
|
||||||
1
cache.go
1
cache.go
|
|
@ -4,4 +4,5 @@ type Cache interface {
|
||||||
Get(key string) (interface{}, error)
|
Get(key string) (interface{}, error)
|
||||||
Set(key string, data interface{}) error
|
Set(key string, data interface{}) error
|
||||||
Remove(key string) error
|
Remove(key string) error
|
||||||
|
Flush()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package gocache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func (cache *myCache) deleteExpired() {
|
||||||
|
cache.mutex.Lock()
|
||||||
|
defer cache.mutex.Unlock()
|
||||||
|
|
||||||
|
now := time.Now().UnixNano()
|
||||||
|
for k, v := range cache.items {
|
||||||
|
if cache.decisionFunc != nil && !cache.decisionFunc(v.data) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v.expiration > 0 && now > v.expiration {
|
||||||
|
if cache.onEviction != nil {
|
||||||
|
cache.onEviction(k, v.data)
|
||||||
|
}
|
||||||
|
delete(cache.items, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,5 +3,5 @@ package gocache
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrKeyNotFound = errors.New("Key not found")
|
ErrKeyNotFound = errors.New("key not found")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
package gocache
|
||||||
|
|
||||||
|
func (cache *myCache) Flush() {
|
||||||
|
cache.mutex.Lock()
|
||||||
|
defer cache.mutex.Unlock()
|
||||||
|
|
||||||
|
cache.items = make(map[string]*value)
|
||||||
|
}
|
||||||
2
get.go
2
get.go
|
|
@ -9,5 +9,5 @@ func (cache *myCache) Get(key string) (interface{}, error) {
|
||||||
return nil, ErrKeyNotFound
|
return nil, ErrKeyNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return item, nil
|
return item.data, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
my_cache.go
26
my_cache.go
|
|
@ -2,11 +2,15 @@ package gocache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type myCache struct {
|
type myCache struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
items map[string]*value
|
items map[string]*value
|
||||||
|
expireAfter int64
|
||||||
|
decisionFunc func(v interface{}) bool
|
||||||
|
onEviction func(k string, v interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCache() Cache {
|
func NewCache() Cache {
|
||||||
|
|
@ -14,3 +18,21 @@ func NewCache() Cache {
|
||||||
items: make(map[string]*value),
|
items: make(map[string]*value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewCacheWithSweeper(interval, expireAfter time.Duration,
|
||||||
|
decisionFunc func(v interface{}) bool, onEviction func(k string, v interface{})) Cache {
|
||||||
|
c := &myCache{
|
||||||
|
items: make(map[string]*value),
|
||||||
|
expireAfter: expireAfter.Nanoseconds(),
|
||||||
|
decisionFunc: decisionFunc,
|
||||||
|
onEviction: onEviction,
|
||||||
|
}
|
||||||
|
|
||||||
|
if interval > 0 && expireAfter > 0 {
|
||||||
|
s := newSweeper(c, interval)
|
||||||
|
// runtime.SetFinalizer(c, s.stopSweeper)
|
||||||
|
go s.run(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
|
||||||
4
set.go
4
set.go
|
|
@ -1,10 +1,12 @@
|
||||||
package gocache
|
package gocache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
func (cache *myCache) Set(key string, data interface{}) error {
|
func (cache *myCache) Set(key string, data interface{}) error {
|
||||||
cache.mutex.Lock()
|
cache.mutex.Lock()
|
||||||
defer cache.mutex.Unlock()
|
defer cache.mutex.Unlock()
|
||||||
|
|
||||||
value := newValue(key, data)
|
value := newValue(key, data, time.Now().UnixNano()+cache.expireAfter)
|
||||||
cache.items[key] = value
|
cache.items[key] = value
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package gocache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type sweeper struct {
|
||||||
|
Interval time.Duration
|
||||||
|
stop chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sweeper) run(c *myCache) {
|
||||||
|
ticker := time.NewTicker(s.Interval)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
c.deleteExpired()
|
||||||
|
case <-s.stop:
|
||||||
|
ticker.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSweeper(c *myCache, i time.Duration) *sweeper {
|
||||||
|
return &sweeper{
|
||||||
|
Interval: i,
|
||||||
|
stop: make(chan bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sweeper) stopSweeper() {
|
||||||
|
s.stop <- true
|
||||||
|
}
|
||||||
12
value.go
12
value.go
|
|
@ -1,13 +1,15 @@
|
||||||
package gocache
|
package gocache
|
||||||
|
|
||||||
type value struct {
|
type value struct {
|
||||||
key string
|
key string
|
||||||
data interface{}
|
data interface{}
|
||||||
|
expiration int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newValue(key string, data interface{}) *value {
|
func newValue(key string, data interface{}, expiration int64) *value {
|
||||||
return &value{
|
return &value{
|
||||||
key: key,
|
key: key,
|
||||||
data: data,
|
data: data,
|
||||||
|
expiration: expiration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue