Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aidenwallis/hotcache
A small hash-map cache in Golang designed for small TTL.
https://github.com/aidenwallis/hotcache
Last synced: about 5 hours 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 (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-29T16:27:08.000Z (about 5 years ago)
- Last Synced: 2024-06-20T16:31:59.733Z (8 months ago)
- Language: Go
- Size: 124 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hotcache
[![GoDoc](https://godoc.org/github.com/aidenwallis/hotcache?status.svg)](https://godoc.org/github.com/aidenwallis/hotcache)
[![Build Status](https://travis-ci.org/aidenwallis/hotcache.svg?branch=master)](https://travis-ci.org/aidenwallis/hotcache)
[![Coverage Status](https://coveralls.io/repos/github/aidenwallis/hotcache/badge.svg?branch=master)](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!")
}
}
```