Skip to content

Commit d06a0aa

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

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed

README.md

+251
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,254 @@ func main() {
397397
}
398398
}
399399
```
400+
401+
### Advanced Features
402+
403+
`SetCapacity(capacity int)`: Dynamically adjust the cache capacity.
404+
405+
eg.
406+
407+
```go
408+
package main
409+
410+
import (
411+
"fmt"
412+
"github.com/pnguyen215/cachify"
413+
)
414+
415+
func main() {
416+
cache := cachify.NewLRU(2)
417+
418+
cache.Set("a", "alpha")
419+
cache.Set("b", "beta")
420+
421+
// Increase capacity to 3
422+
cache.SetCapacity(3)
423+
cache.Set("c", "gamma")
424+
425+
fmt.Println("Cache state after capacity increase:", cache.GetAll())
426+
// Output: Cache state after capacity increase: map[a:alpha b:beta c:gamma]
427+
428+
// Decrease capacity back to 2, evicting the least recently used ('a')
429+
cache.SetCapacity(2)
430+
fmt.Println("Cache state after capacity decrease:", cache.GetAll())
431+
// Output: Cache state after capacity decrease: map[b:beta c:gamma]
432+
}
433+
```
434+
435+
`SetCallback(callback OnCallback)`: Set or update the eviction callback function.
436+
437+
eg.
438+
439+
```go
440+
package main
441+
442+
import (
443+
"fmt"
444+
"github.com/pnguyen215/cachify"
445+
)
446+
447+
func evictionLogger(key string, value interface{}) {
448+
fmt.Printf("Evicted: Key=%s, Value=%v\n", key, value)
449+
}
450+
451+
func main() {
452+
cache := cachify.NewLRU(2)
453+
454+
// Set a callback for evictions
455+
cache.SetCallback(evictionLogger)
456+
457+
cache.Set("x", "X-ray")
458+
cache.Set("y", "Yankee")
459+
cache.Set("z", "Zulu")
460+
// Output: Evicted: Key=x, Value=X-ray
461+
}
462+
```
463+
464+
`SetExpiry(expiry time.Duration)`: Update the expiration time for all cache entries.
465+
466+
eg.
467+
468+
```go
469+
package main
470+
471+
import (
472+
"fmt"
473+
"time"
474+
"github.com/pnguyen215/cachify"
475+
)
476+
477+
func main() {
478+
cache := cachify.NewLRUExpires(2, 5*time.Second)
479+
480+
cache.Set("a", "alpha")
481+
482+
// Update the expiration time to 10 seconds
483+
cache.SetExpiry(10 * time.Second)
484+
485+
time.Sleep(2 * time.Second)
486+
if val, ok := cache.Get("a"); ok {
487+
fmt.Println("Value still exists:", val) // Output: Value still exists: alpha
488+
}
489+
}
490+
```
491+
492+
`GetStates()`: Get metadata for all entries.
493+
494+
eg.
495+
496+
```go
497+
package main
498+
499+
import (
500+
"fmt"
501+
502+
"github.com/pnguyen215/cachify"
503+
)
504+
505+
func main() {
506+
cache := cachify.NewLRU(3)
507+
508+
cache.Set("x", "X-ray")
509+
cache.Set("y", "Yankee")
510+
cache.Set("z", "Zulu")
511+
512+
// Retrieve metadata for all entries
513+
for _, state := range cache.GetStates() {
514+
fmt.Printf("Key: %v, Value: %v, LastAccess: %v\n", state.Key(), state.Value(), state.AccessTime())
515+
}
516+
}
517+
```
518+
519+
`GetState()`: Get metadata for the least recently used item.
520+
521+
eg.
522+
523+
```go
524+
package main
525+
526+
import (
527+
"fmt"
528+
529+
"github.com/pnguyen215/cachify"
530+
)
531+
532+
func main() {
533+
cache := cachify.NewLRU(3)
534+
535+
cache.Set("x", "X-ray")
536+
cache.Set("y", "Yankee")
537+
538+
// Retrieve metadata for the least recently used item
539+
if state, ok := cache.GetState(); ok {
540+
fmt.Printf("LRU State: Key=%s, Value=%v, LastAccess=%v\n", state.Key(), state.Value(), state.AccessTime())
541+
}
542+
}
543+
```
544+
545+
`IsMostRecentlyUsed(key string)`: Check if a key is the most recently used.
546+
547+
eg.
548+
549+
```go
550+
package main
551+
552+
import (
553+
"fmt"
554+
"github.com/pnguyen215/cachify"
555+
)
556+
557+
func main() {
558+
cache := cachify.NewLRU(3)
559+
560+
cache.Set("a", "alpha")
561+
cache.Set("b", "beta")
562+
cache.Set("c", "gamma")
563+
564+
if cache.IsMostRecentlyUsed("c") {
565+
fmt.Println("Key 'c' is the most recently used.") // Output: Key 'c' is the most recently used.
566+
}
567+
}
568+
```
569+
570+
`GetMostRecentlyUsed()`: Retrieve the most recently used item.
571+
572+
eg.
573+
574+
```go
575+
package main
576+
577+
import (
578+
"fmt"
579+
580+
"github.com/pnguyen215/cachify"
581+
)
582+
583+
func main() {
584+
cache := cachify.NewLRU(3)
585+
586+
cache.Set("a", "alpha")
587+
cache.Set("b", "beta")
588+
cache.Set("c", "gamma")
589+
590+
// Retrieve the most recently used item
591+
if state, ok := cache.GetMostRecentlyUsed(); ok {
592+
fmt.Printf("Most Recently Used: Key=%s, Value=%v\n", state.Key(), state.Value())
593+
// Output: Most Recently Used: Key=c, Value=gamma
594+
}
595+
}
596+
```
597+
598+
`ExpandExpiry(key string, expiry time.Duration)`: Extend the expiration time for a specific key.
599+
600+
eg.
601+
602+
```go
603+
package main
604+
605+
import (
606+
"fmt"
607+
"time"
608+
"github.com/pnguyen215/cachify"
609+
)
610+
611+
func main() {
612+
cache := cachify.NewLRUExpires(2, 5*time.Second)
613+
614+
cache.Set("a", "alpha")
615+
616+
// Extend the expiration time by 10 seconds
617+
cache.ExpandExpiry("a", 10*time.Second)
618+
619+
time.Sleep(2 * time.Second)
620+
if val, ok := cache.Get("a"); ok {
621+
fmt.Println("Value after extended expiry:", val) // Output: Value after extended expiry: alpha
622+
}
623+
}
624+
```
625+
626+
`PersistExpiry(key string)`: Retrieve the remaining time until expiration for a key.
627+
628+
eg.
629+
630+
```go
631+
package main
632+
633+
import (
634+
"fmt"
635+
"time"
636+
"github.com/pnguyen215/cachify"
637+
)
638+
639+
func main() {
640+
cache := cachify.NewLRUExpires(2, 10*time.Second)
641+
642+
cache.Set("a", "alpha")
643+
644+
// Check remaining time for expiration
645+
if remain, ok := cache.PersistExpiry("a"); ok {
646+
fmt.Printf("Time until expiration: %v\n", remain)
647+
// Output: Time until expiration: ~9s (actual value may vary slightly)
648+
}
649+
}
650+
```

0 commit comments

Comments
 (0)