https://github.com/hunterhug/gocache
💐Memory Cache Implement By Golang
https://github.com/hunterhug/gocache
cache cache-storage gocache kvstore memorycache minheap rbtree treemap
Last synced: 4 days ago
JSON representation
💐Memory Cache Implement By Golang
- Host: GitHub
- URL: https://github.com/hunterhug/gocache
- Owner: hunterhug
- License: apache-2.0
- Created: 2021-08-30T09:15:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T09:29:25.000Z (over 4 years ago)
- Last Synced: 2024-06-20T14:21:52.826Z (over 1 year ago)
- Topics: cache, cache-storage, gocache, kvstore, memorycache, minheap, rbtree, treemap
- Language: Go
- Homepage:
- Size: 71.3 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Memory Cache auto clean expired data implement by Golang
[](https://github.com/hunterhug/gocache/network)
[](https://github.com/hunterhug/gocache/stargazers)
[](https://github.com/hunterhug/gocache)
[](https://github.com/hunterhug/gocache/issues)
[中文说明](/README_ZH.md)
I use Red-Black Tree Map and Minimum Heap keep the data in memory (the root node of Minimum Heap has the oldest value, so we can fast clean the expired values).
Cache in memory implement very efficient, No pre allocated space required.
## Usage
Simple get it by:
```
go get -v github.com/hunterhug/gocache
```
## Demo
Follow the interface method:
```go
type Cache interface {
Set(key string, value []byte, expireTime time.Duration)
SetInterface(key string, value interface{}, expireTime time.Duration)
SetByExpireUnixNanosecondDateTime(key string, value []byte, expireUnixNanosecondDateTime int64)
SetInterfaceByExpireUnixNanosecondDateTime(key string, value interface{}, expireUnixNanosecondDateTime int64)
Delete(key string)
Get(key string) (value []byte, expireUnixNanosecondDateTime int64, exist bool)
GetInterface(key string) (value interface{}, expireUnixNanosecondDateTime int64, exist bool)
GetOldestKey() (key string, expireUnixNanosecondDateTime int64, exist bool)
Size() int
Index(index int) (value []byte, expireUnixNanosecondDateTime int64, exist bool)
IndexInterface(index int) (value interface{}, expireUnixNanosecondDateTime int64, exist bool)
KeyList() []string
ShutDown()
}
```
You can choose to set cache with expireTime `time.Duration = 1 minute` by call `Set(key string, value []byte, expireTime time.Duration)` or other method.
Example:
```go
package main
import (
"fmt"
"github.com/hunterhug/gocache"
"time"
)
func main() {
cache := gocache.New()
defer cache.ShutDown()
cache.Set("a", []byte("a hi"), 2*time.Second)
cache.Set("b", []byte("b hi"), 2*time.Second)
cache.SetInterface("c", []byte("c hi"), 2*time.Second)
fmt.Println(cache.Size())
fmt.Println(cache.GetOldestKey())
fmt.Println(cache.KeyList())
fmt.Println(cache.Get("a"))
fmt.Println(cache.GetInterface("c"))
time.Sleep(2 * time.Second)
fmt.Println(cache.Get("a"))
}
```
# License
```
Copyright [2019-2021] [github.com/hunterhug]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```