https://github.com/aidenwallis/hotcache
A small hash-map cache in Golang designed for small TTL.
https://github.com/aidenwallis/hotcache
Last synced: 10 days ago
JSON representation
A small hash-map cache in Golang designed for small TTL.
- Host: GitHub
- URL: https://github.com/aidenwallis/hotcache
- Owner: aidenwallis
- License: mit
- Created: 2020-01-29T13:05:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-29T16:27:08.000Z (over 5 years ago)
- Last Synced: 2025-04-02T15:43:41.857Z (3 months ago)
- Language: Go
- Size: 124 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hotcache
[](https://godoc.org/github.com/aidenwallis/hotcache)
[](https://travis-ci.org/aidenwallis/hotcache)
[](https://coveralls.io/github/aidenwallis/hotcache?branch=master)A small hash-map cache in Golang designed for small TTL. It's a simple hashmap implementation that allows you to effectively handle TTL on temporary keys. TTL works on last set wins, meaning if you set a longer TTL on a key, it will expire when the new TTL is set.
It invalidates keys by checking 1000 random keys in store every 100ms, as well as does an expiry check per lookup/set.
Hotcache is completely thread-safe due to its use of RWMutexes, therefore you don't need to be concerned with doing that yourself. I originally wrote this package months ago and chose to make it public to just make my life easier for [Fossabot](https://fossabot.com).
## Usage
This package is incredibly easy to use, like so
```go
package mainimport (
"fmt""github.com/aidenwallis/hotcache"
)func main() {
cache := hotcache.New()cache.Set("key", "value", time.Second*2)
value, ok := cache.Get("key")
if ok {
// This key exists in the hashmap.
fmt.Println("Key value: " + value.(string))
}// Sleep until cache expires.
time.Sleep(time.Second * 2)
exists := cache.Has("key")
if !exists {
fmt.Println("Key has now expired!")
}
}
```