Skip to content

Commit

Permalink
examples: Fixed/updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jahnestacado committed Jun 18, 2024
1 parent 101d16f commit 7bf3d60
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 134 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.13', '1.14', '1.15', '1.16', '1.17', '1.18' ]
go: [ '1.18', '1.19', '1.20', '1.21', '1.22' ]
name: Go ${{ matrix.go }} sample
steps:
- uses: actions/checkout@v2
Expand All @@ -29,6 +29,9 @@ jobs:
fail_ci_if_error: true
verbose: true

- name: Run examples
run: make test.examples

- name: Run linter
run: make lint

Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ help:
test:
go test -race -v -coverprofile=coverage.txt -covermode=atomic

#test.examples: @ Runs examples
test.examples:
go test -race -v ./examples

#bench: @ Runs performance tests
bench:
go test -bench=.
Expand Down
125 changes: 125 additions & 0 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"fmt"
"time"

"github.com/jahnestacado/tlru"
)

var (
entry1 = tlru.Entry[string, int]{Key: "entry-1", Value: 1}
entry2 = tlru.Entry[string, int]{Key: "entry-2", Value: 2}
entry3 = tlru.Entry[string, int]{Key: "entry-3", Value: 3}
entry4 = tlru.Entry[string, int]{Key: "entry-4", Value: 4}
entry5 = tlru.Entry[string, int]{Key: "entry-5", Value: 5}

ttl = 2 * time.Millisecond
)

func ExampleLRA() {
evictionChannel := make(chan tlru.EvictedEntry[string, int])
config := tlru.Config[string, int]{
MaxSize: 2,
TTL: ttl,
EvictionPolicy: tlru.LRA,
EvictionChannel: &evictionChannel,
GarbageCollectionInterval: ttl,
}
cache := tlru.New(config)

go func() {
for {
evictedEntry := <-evictionChannel
fmt.Printf("Entry with key: '%s' has been evicted with reason: %s\n", evictedEntry.Key, evictedEntry.Reason.String())
}
}()

cache.Set(entry1.Key, entry1.Value)
time.Sleep(2 * ttl)
cache.Set(entry2.Key, entry2.Value)
cache.Set(entry3.Key, entry3.Value)
cache.Set(entry4.Key, entry4.Value)
cache.Delete(entry4.Key)
cache.Set(entry5.Key, entry5.Value)

// Duplicate keys are not allowed in LRA
err := cache.Set(entry5.Key, entry5.Value)
if err != nil {
fmt.Println(err.Error())
}

fmt.Printf("Number of Keys in cache: %d\n", len(cache.Keys()))
fmt.Printf("Entry with key: 'entry-3' is in cache: %t\n", cache.Has(entry3.Key))
fmt.Printf("Entry with key: 'entry-5' is in cache: %t\n", cache.Has(entry5.Key))

cache.Get(entry3.Key)
cachedEntry3 := cache.Get(entry3.Key)
fmt.Printf("Entry with key: '%s' has been accessed %d times\n", entry3.Key, cachedEntry3.Counter)

cache.Get(entry5.Key)
cache.Get(entry5.Key)
cachedEntry5 := cache.Get(entry5.Key)
fmt.Printf("Entry with key: '%s' has been accessed %d times\n", entry5.Key, cachedEntry5.Counter)

// Output:
// Entry with key: 'entry-1' has been evicted with reason: Expired
// Entry with key: 'entry-2' has been evicted with reason: Dropped
// Entry with key: 'entry-4' has been evicted with reason: Deleted
// tlru.Set: Key 'entry-5' already exist. Entry replacement is not allowed in LRA EvictionPolicy
// Number of Keys in cache: 2
// Entry with key: 'entry-3' is in cache: true
// Entry with key: 'entry-5' is in cache: true
// Entry with key: 'entry-3' has been accessed 2 times
// Entry with key: 'entry-5' has been accessed 3 times
}

func ExampleLRI() {
evictionChannel := make(chan tlru.EvictedEntry[string, int])
config := tlru.Config[string, int]{
MaxSize: 3,
TTL: ttl,
EvictionPolicy: tlru.LRI,
EvictionChannel: &evictionChannel,
GarbageCollectionInterval: ttl,
}
cache := tlru.New(config)

go func() {
for {
evictedEntry := <-evictionChannel
fmt.Printf("Entry with key: '%s' has been evicted with reason: %s\n", evictedEntry.Key, evictedEntry.Reason.String())
}
}()

cache.Set(entry1.Key, entry1.Value)
time.Sleep(2 * ttl)
cache.Set(entry2.Key, entry2.Value)
cache.Set(entry3.Key, entry3.Value)
cache.Set(entry2.Key, entry2.Value)
cache.Set(entry4.Key, entry4.Value)
cache.Set(entry4.Key, entry4.Value)
cache.Set(entry5.Key, entry5.Value)
cache.Set(entry4.Key, entry4.Value)

cache.Delete(entry5.Key)

fmt.Printf("Number of Keys in cache: %d\n", len(cache.Keys()))
fmt.Printf("Entry with key: 'entry-2' is in cache: %t\n", cache.Has(entry2.Key))
fmt.Printf("Entry with key: 'entry-4' is in cache: %t\n", cache.Has(entry4.Key))

cachedEntry2 := cache.Get(entry2.Key)
fmt.Printf("Entry with key: '%s' has been inserted %d times\n", entry2.Key, cachedEntry2.Counter)
cachedEntry4 := cache.Get(entry4.Key)
fmt.Printf("Entry with key: '%s' has been inserted %d times\n", entry4.Key, cachedEntry4.Counter)

// Output:
// Entry with key: 'entry-1' has been evicted with reason: Expired
// Entry with key: 'entry-3' has been evicted with reason: Dropped
// Entry with key: 'entry-5' has been evicted with reason: Deleted
// Number of Keys in cache: 2
// Entry with key: 'entry-2' is in cache: true
// Entry with key: 'entry-4' is in cache: true
// Entry with key: 'entry-2' has been inserted 2 times
// Entry with key: 'entry-4' has been inserted 3 times
}
70 changes: 0 additions & 70 deletions examples/lra/tlru_lra.go

This file was deleted.

63 changes: 0 additions & 63 deletions examples/lri/tlru_lri.go

This file was deleted.

0 comments on commit 7bf3d60

Please sign in to comment.