Skip to content

Commit 4d2854b

Browse files
committed
chore: update readme
1 parent d313609 commit 4d2854b

File tree

1 file changed

+202
-1
lines changed

1 file changed

+202
-1
lines changed

README.md

+202-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,202 @@
1-
# memc
1+
[![GoDoc](https://godoc.org/github.com/shaj13/go-guardian/v2?status.svg)](https://pkg.go.dev/github.com/shaj13/go-guardian/v2)
2+
[![Go Report Card](https://goreportcard.com/badge/github.com/shaj13/go-guardian)](https://goreportcard.com/report/github.com/shaj13/go-guardian)
3+
[![Coverage Status](https://coveralls.io/repos/github/shaj13/go-guardian/badge.svg?branch=master)](https://coveralls.io/github/shaj13/go-guardian?branch=master)
4+
[![CircleCI](https://circleci.com/gh/shaj13/go-guardian/tree/master.svg?style=svg)](https://circleci.com/gh/shaj13/go-guardian/tree/master)
5+
6+
# Libcache
7+
A Lightweight in-memory key:value cache library for Go.
8+
9+
## Introduction
10+
Caches are tremendously useful in a wide variety of use cases.<br>
11+
you should consider using caches when a value is expensive to compute or retrieve,<br>
12+
and you will need its value on a certain input more than once.<br>
13+
libcache is here to help with that.
14+
15+
Libcache are local to a single run of your application.<br>
16+
They do not store data in files, or on outside servers.
17+
18+
Libcache previously an [go-guardian](https://github.com/shaj13/go-guardian) package and designed to be a companion with it.<br>
19+
While both can operate completely independently.<br>
20+
21+
22+
## Features
23+
- Rich [caching API]()
24+
- Maximum cache size enforcement
25+
- Default cache TTL (time-to-live) as well as custom TTLs per cache entry
26+
- Thread safe as well as non-thread safe
27+
- Event-Driven callbacks ([OnExpired](),[OnEvicted]())
28+
- Dynamic cache creation
29+
- Multiple cache replacement policies:
30+
- FIFO (First In, First Out)
31+
- LIFO (Last In, First Out)
32+
- LRU (Least Recently Used)
33+
- MRU (Most Recently Used)
34+
- LFU (Least Frequently Used)
35+
- ARC (Adaptive Replacement Cache)
36+
37+
## Quickstart
38+
### Installing
39+
Using libcache is easy. First, use go get to install the latest version of the library.
40+
41+
```sh
42+
go get github.com/shaj13/libcache
43+
```
44+
Next, include libcache in your application:
45+
```go
46+
import (
47+
_ "github.com/shaj13/libcache/<desired-replacement-policy>"
48+
"github.com/shaj13/libcache"
49+
)
50+
```
51+
52+
### Examples
53+
**Note:** All examples use the LRU cache replacement policy for simplicity, any other cache replacement policy can be applied to them.
54+
#### Basic
55+
```go
56+
package main
57+
import (
58+
"fmt"
59+
60+
"github.com/shaj13/libcache"
61+
_ "github.com/shaj13/libcache/lru"
62+
)
63+
64+
func main() {
65+
size := 10
66+
cache := libcache.LRU.NewUnsafe(size)
67+
for i:= 0 ; i < 10 ; i++ {
68+
cache.Store(i, i)
69+
}
70+
fmt.Println(cache.Load(0)) // nil, false
71+
fmt.Println(cache.Load(1)) // 1, true
72+
}
73+
```
74+
75+
#### Thread Safe
76+
```go
77+
package main
78+
79+
import (
80+
"fmt"
81+
82+
"github.com/shaj13/libcache"
83+
_ "github.com/shaj13/libcache/lru"
84+
)
85+
86+
func main() {
87+
done := make(chan struct{})
88+
89+
f := func(c libcache.Cache) {
90+
for !c.Contains(5) {
91+
}
92+
fmt.Println(c.Load(5)) // 5, true
93+
done <- struct{}{}
94+
}
95+
96+
size := 10
97+
cache := libcache.LRU.New(size)
98+
go f(cache)
99+
100+
for i := 0; i < 10; i++ {
101+
cache.Store(i, i)
102+
}
103+
104+
<-done
105+
}
106+
```
107+
#### Unlimited Size
108+
zero capacity means cache has no limit and replacement policy turned off.
109+
```go
110+
package main
111+
import (
112+
"fmt"
113+
114+
"github.com/shaj13/libcache"
115+
_ "github.com/shaj13/libcache/lru"
116+
)
117+
118+
func main() {
119+
cache := libcache.LRU.New(0)
120+
for i:= 0 ; i < 100000 ; i++ {
121+
cache.Store(i, i)
122+
}
123+
fmt.Println(cache.Load(55555))
124+
}
125+
```
126+
#### TTL
127+
```go
128+
package main
129+
import (
130+
"fmt"
131+
"time"
132+
133+
"github.com/shaj13/libcache"
134+
_ "github.com/shaj13/libcache/lru"
135+
)
136+
137+
func main() {
138+
cache := libcache.LRU.New(10)
139+
cache.SetTTL(time.Second) // default TTL
140+
141+
for i:= 0 ; i < 10 ; i++ {
142+
cache.Store(i, i)
143+
}
144+
fmt.Println(cache.Expiry(1))
145+
146+
cache.StoreWithTTL("mykey", "value", time.Hour) // TTL per cache entry
147+
fmt.Println(cache.Expiry("mykey"))
148+
149+
}
150+
```
151+
152+
#### Events
153+
Timed expiration by default is performed with lazy maintenance during reads operations,<br>
154+
Evict an expired entry immediately can be done using on expired callback.<br>
155+
You may also specify a eviction listener for your cache to perform some operation when an entry is evicted.<br>
156+
**Note:** Expiration events relying on golang runtime timers heap to call on expired callback when an entry TTL elapsed.
157+
```go
158+
package main
159+
import (
160+
"fmt"
161+
"time"
162+
163+
"github.com/shaj13/libcache"
164+
_ "github.com/shaj13/libcache/lru"
165+
)
166+
167+
func main() {
168+
cache := libcache.LRU.New(10)
169+
cache.RegisterOnEvicted(func(key, value interface{}) {
170+
fmt.Printf("Cache Key %v Evicted\n", key)
171+
})
172+
173+
cache.RegisterOnExpired(func(key, value interface{}) {
174+
fmt.Printf("Cache Key %v Expired, Removing it from cache\n", key)
175+
// use delete directly when your application
176+
// guarantee no other goroutine can store items with the same key.
177+
// Peek also invoke lazy expiry.
178+
//
179+
// Note this should done only with safe cache.
180+
cache.Peek(key)
181+
})
182+
183+
for i:= 0 ; i < 10 ; i++ {
184+
cache.StoreWithTTL(i, i, time.Microsecond)
185+
}
186+
187+
time.Sleep(time.Second)
188+
fmt.Println(cache.Len())
189+
}
190+
```
191+
192+
# Contributing
193+
1. Fork it
194+
2. Download your fork to your PC (`git clone https://github.com/your_username/libcache && cd libcache`)
195+
3. Create your feature branch (`git checkout -b my-new-feature`)
196+
4. Make changes and add them (`git add .`)
197+
5. Commit your changes (`git commit -m 'Add some feature'`)
198+
6. Push to the branch (`git push origin my-new-feature`)
199+
7. Create new pull request
200+
201+
# License
202+
Libcache is released under the MIT license. See [LICENSE](https://github.com/shaj13/libcache/blob/master/LICENSE)

0 commit comments

Comments
 (0)