Skip to content

Commit d8cefd6

Browse files
committed
📚 docs: update README.md #4
1 parent 3754143 commit d8cefd6

File tree

1 file changed

+328
-0
lines changed

1 file changed

+328
-0
lines changed

README.md

+328
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,331 @@ To start using `cachify`, run `go get`:
6969
- `GetMostRecentlyUsed() (state *state, ok bool)`: Retrieve the most recently used item.
7070
- `ExpandExpiry(key string, expiry time.Duration)`: Extend the expiration time for a key.
7171
- `PersistExpiry(key string) (remain time.Duration, ok bool)`: PersistExpiry returns the remaining time until expiration for a specific key.
72+
73+
## Usage
74+
75+
### Cache Initialization
76+
77+
`NewLRU(capacity int)`: Create an LRU cache with a fixed capacity.
78+
79+
eg.
80+
81+
```go
82+
package main
83+
84+
import (
85+
"fmt"
86+
"github.com/pnguyen215/cachify"
87+
)
88+
89+
func main() {
90+
// Create an LRU cache with a capacity of 3
91+
cache := cachify.NewLRU(3)
92+
93+
// Add entries to the cache
94+
cache.Set("a", "alpha")
95+
cache.Set("b", "beta")
96+
cache.Set("c", "gamma")
97+
98+
// Adding another entry will evict the least recently used ('a')
99+
cache.Set("d", "delta")
100+
101+
// Inspect the cache state
102+
fmt.Println(cache.GetAll())
103+
// Output: map[b:beta c:gamma d:delta]
104+
}
105+
```
106+
107+
`NewLRUCallback(capacity int, callback OnCallback)`: Create an LRU cache with a custom eviction callback.
108+
109+
eg.
110+
111+
```go
112+
package main
113+
114+
import (
115+
"fmt"
116+
"github.com/pnguyen215/cachify"
117+
)
118+
119+
func onEviction(key string, value interface{}) {
120+
fmt.Printf("Evicted: Key=%s, Value=%v\n", key, value)
121+
}
122+
123+
func main() {
124+
// Create an LRU cache with a capacity of 2 and a custom callback
125+
cache := cachify.NewLRUCallback(2, onEviction)
126+
127+
cache.Set("x", "X-ray")
128+
cache.Set("y", "Yankee")
129+
130+
// Adding a third entry triggers eviction of the least recently used ('x')
131+
cache.Set("z", "Zulu")
132+
// Output: Evicted: Key=x, Value=X-ray
133+
}
134+
```
135+
136+
`NewLRUExpires(capacity int, expiry time.Duration)`: Create an LRU cache with expiration.
137+
138+
eg.
139+
140+
```go
141+
package main
142+
143+
import (
144+
"fmt"
145+
"time"
146+
"github.com/pnguyen215/cachify"
147+
)
148+
149+
func main() {
150+
// Create an LRU cache with expiration (5 seconds)
151+
cache := cachify.NewLRUExpires(2, 5*time.Second)
152+
153+
cache.Set("k", "keep")
154+
155+
// Retrieve the key before expiration
156+
if val, ok := cache.Get("k"); ok {
157+
fmt.Println("Value:", val) // Output: Value: keep
158+
}
159+
160+
// Wait for the key to expire
161+
time.Sleep(6 * time.Second)
162+
163+
// Attempt to retrieve the key after expiration
164+
if _, ok := cache.Get("k"); !ok {
165+
fmt.Println("Key expired.") // Output: Key expired.
166+
}
167+
}
168+
```
169+
170+
### Cache Operations
171+
172+
`Get(key string)`: Retrieve an entry by key.
173+
174+
eg.
175+
176+
```go
177+
package main
178+
179+
import (
180+
"fmt"
181+
"github.com/pnguyen215/cachify"
182+
)
183+
184+
func main() {
185+
cache := cachify.NewLRU(3)
186+
cache.Set("a", "alpha")
187+
188+
// Retrieve an existing key
189+
if value, ok := cache.Get("a"); ok {
190+
fmt.Println("Value:", value) // Output: Value: alpha
191+
}
192+
193+
// Attempt to retrieve a non-existing key
194+
if _, ok := cache.Get("b"); !ok {
195+
fmt.Println("Key not found.") // Output: Key not found.
196+
}
197+
}
198+
```
199+
200+
`GetAll()`: Retrieve all key-value pairs.
201+
202+
eg.
203+
204+
```go
205+
package main
206+
207+
import (
208+
"fmt"
209+
"github.com/pnguyen215/cachify"
210+
)
211+
212+
func main() {
213+
cache := cachify.NewLRU(3)
214+
cache.Set("a", "alpha")
215+
cache.Set("b", "beta")
216+
217+
// Get all key-value pairs
218+
fmt.Println("Cache state:", cache.GetAll())
219+
// Output: Cache state: map[a:alpha b:beta]
220+
}
221+
```
222+
223+
`Set(key string, value interface{})`: Add or update an entry.
224+
225+
eg.
226+
227+
```go
228+
package main
229+
230+
import (
231+
"fmt"
232+
"github.com/pnguyen215/cachify"
233+
)
234+
235+
func main() {
236+
cache := cachify.NewLRU(2)
237+
238+
// Add entries to the cache
239+
cache.Set("a", "alpha")
240+
cache.Set("b", "beta")
241+
242+
// Update an existing key
243+
cache.Set("a", "updated_alpha")
244+
245+
// Inspect the cache state
246+
fmt.Println(cache.GetAll())
247+
// Output: map[a:updated_alpha b:beta]
248+
}
249+
```
250+
251+
`Update(key string, value interface{})`: Update an existing entry without altering order.
252+
253+
eg.
254+
255+
```go
256+
package main
257+
258+
import (
259+
"fmt"
260+
"github.com/pnguyen215/cachify"
261+
)
262+
263+
func main() {
264+
cache := cachify.NewLRU(3)
265+
266+
cache.Set("x", "X-ray")
267+
cache.Update("x", "Updated X-ray")
268+
269+
// Confirm the update
270+
if value, ok := cache.Get("x"); ok {
271+
fmt.Println("Updated value:", value) // Output: Updated value: Updated X-ray
272+
}
273+
}
274+
```
275+
276+
`Remove(key string)`: Remove a specific entry.
277+
278+
eg.
279+
280+
```go
281+
package main
282+
283+
import (
284+
"fmt"
285+
"github.com/pnguyen215/cachify"
286+
)
287+
288+
func main() {
289+
cache := cachify.NewLRU(3)
290+
291+
cache.Set("a", "alpha")
292+
cache.Set("b", "beta")
293+
294+
// Remove an entry
295+
cache.Remove("a")
296+
297+
// Attempt to retrieve the removed entry
298+
if _, ok := cache.Get("a"); !ok {
299+
fmt.Println("Key 'a' removed.") // Output: Key 'a' removed.
300+
}
301+
}
302+
```
303+
304+
`Clear()`: Clear all entries from the cache.
305+
306+
eg.
307+
308+
```go
309+
package main
310+
311+
import (
312+
"fmt"
313+
"github.com/pnguyen215/cachify"
314+
)
315+
316+
func main() {
317+
cache := cachify.NewLRU(3)
318+
319+
cache.Set("x", "X-ray")
320+
cache.Set("y", "Yankee")
321+
322+
// Clear the cache
323+
cache.Clear()
324+
325+
// Confirm the cache is empty
326+
if cache.Len() == 0 {
327+
fmt.Println("Cache cleared.") // Output: Cache cleared.
328+
}
329+
}
330+
```
331+
332+
`Len()`: Get the number of entries in the cache.
333+
334+
eg.
335+
336+
```go
337+
package main
338+
339+
import (
340+
"fmt"
341+
"github.com/pnguyen215/cachify"
342+
)
343+
344+
func main() {
345+
cache := cachify.NewLRU(3)
346+
347+
cache.Set("a", "alpha")
348+
cache.Set("b", "beta")
349+
350+
fmt.Println("Cache length:", cache.Len())
351+
// Output: Cache length: 2
352+
}
353+
```
354+
355+
`IsEmpty()`: Check if the cache is empty.
356+
357+
eg.
358+
359+
```go
360+
package main
361+
362+
import (
363+
"fmt"
364+
"github.com/pnguyen215/cachify"
365+
)
366+
367+
func main() {
368+
cache := cachify.NewLRU(3)
369+
370+
fmt.Println("Is cache empty?", cache.IsEmpty())
371+
// Output: Is cache empty? true
372+
}
373+
```
374+
375+
`Pairs()`: Retrieve the least recently used pair.
376+
377+
eg.
378+
379+
```go
380+
package main
381+
382+
import (
383+
"fmt"
384+
"github.com/pnguyen215/cachify"
385+
)
386+
387+
func main() {
388+
cache := cachify.NewLRU(3)
389+
390+
cache.Set("a", "alpha")
391+
cache.Set("b", "beta")
392+
393+
// Retrieve the least recently used pair
394+
if key, value, ok := cache.Pairs(); ok {
395+
fmt.Printf("Least Recently Used: Key=%s, Value=%v\n", key, value)
396+
// Output: Least Recently Used: Key=a, Value=alpha
397+
}
398+
}
399+
```

0 commit comments

Comments
 (0)