mirror of https://github.com/rjbasitali/go-cache
Compare commits
No commits in common. "master" and "v0.0.5" have entirely different histories.
21
LICENSE
21
LICENSE
|
|
@ -1,21 +0,0 @@
|
||||||
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.
|
|
||||||
50
README.md
50
README.md
|
|
@ -1,50 +0,0 @@
|
||||||
# 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,6 +1,7 @@
|
||||||
package gocache
|
package gocache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -30,7 +31,7 @@ func NewCacheWithSweeper(interval, expireAfter time.Duration,
|
||||||
|
|
||||||
if interval > 0 && expireAfter > 0 {
|
if interval > 0 && expireAfter > 0 {
|
||||||
s := newSweeper(c, interval)
|
s := newSweeper(c, interval)
|
||||||
// runtime.SetFinalizer(c, s.stopSweeper)
|
runtime.SetFinalizer(c, s.stopSweeper)
|
||||||
go s.run(c)
|
go s.run(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue