Skip to content

Commit 9835139

Browse files
authored
Merge pull request #15 from Code-Hex/add/policy
added policy directory
2 parents 67b9b3d + 76570d8 commit 9835139

22 files changed

+41
-41
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ go-generics-cache is an in-memory key:value store/cache that is suitable for app
88
- implemented with [Go Generics](https://go.dev/blog/generics-proposal)
99
- TTL supported (with expiration times)
1010
- Simple cache is like `map[string]interface{}`
11-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/simple/example_test.go)
11+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/simple/example_test.go)
1212
- Cache replacement policies
1313
- **Least recently used (LRU)**
1414
- Discards the least recently used items first.
15-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/lru/example_test.go)
15+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lru/example_test.go)
1616
- **Least-frequently used (LFU)**
1717
- Counts how often an item is needed. Those that are used least often are discarded first.
1818
- [An O(1) algorithm for implementing the LFU cache eviction scheme](http://dhruvbird.com/lfu.pdf)
19-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/lfu/example_test.go)
19+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lfu/example_test.go)
2020
- **First in first out (FIFO)**
2121
- Using this algorithm the cache behaves in the same way as a [FIFO queue](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)).
2222
- The cache evicts the blocks in the order they were added, without any regard to how often or how many times they were accessed before.
23-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/fifo/example_test.go)
23+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/fifo/example_test.go)
2424
- **Most recently used (MRU)**
2525
- In contrast to Least Recently Used (LRU), MRU discards the most recently used items first.
26-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/mru/example_test.go)
26+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/mru/example_test.go)
2727
- **Clock**
2828
- Clock is a more efficient version of FIFO than Second-chance cache algorithm.
29-
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/clock/example_test.go)
29+
- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/clock/example_test.go)
3030

3131
## Requirements
3232

cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"sync"
55
"time"
66

7-
"github.com/Code-Hex/go-generics-cache/clock"
8-
"github.com/Code-Hex/go-generics-cache/fifo"
9-
"github.com/Code-Hex/go-generics-cache/lfu"
10-
"github.com/Code-Hex/go-generics-cache/lru"
11-
"github.com/Code-Hex/go-generics-cache/mru"
12-
"github.com/Code-Hex/go-generics-cache/simple"
7+
"github.com/Code-Hex/go-generics-cache/policy/clock"
8+
"github.com/Code-Hex/go-generics-cache/policy/fifo"
9+
"github.com/Code-Hex/go-generics-cache/policy/lfu"
10+
"github.com/Code-Hex/go-generics-cache/policy/lru"
11+
"github.com/Code-Hex/go-generics-cache/policy/mru"
12+
"github.com/Code-Hex/go-generics-cache/policy/simple"
1313
)
1414

1515
// Interface is a common-cache interface.

example_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ExampleCache() {
2020
// 0 false
2121
}
2222

23-
func ExampleCacheAsClock() {
23+
func ExampleAsClock() {
2424
// use clock cache algorithm.
2525
c := cache.New(cache.AsClock[string, int]())
2626
c.Set("a", 1)
@@ -33,7 +33,7 @@ func ExampleCacheAsClock() {
3333
// 0 false
3434
}
3535

36-
func ExampleCacheWithExpiration() {
36+
func ExampleWithExpiration() {
3737
c := cache.New(cache.AsFIFO[string, int]())
3838
exp := 250 * time.Millisecond
3939
c.Set("a", 1, cache.WithExpiration(exp))
@@ -58,7 +58,7 @@ func ExampleCacheWithExpiration() {
5858
// 0 false
5959
}
6060

61-
func ExampleDelete() {
61+
func ExampleCache_Delete() {
6262
c := cache.New(cache.AsMRU[string, int]())
6363
c.Set("a", 1)
6464
c.Delete("a")
@@ -68,7 +68,7 @@ func ExampleDelete() {
6868
// 0 false
6969
}
7070

71-
func ExampleKeys() {
71+
func ExampleCache_Keys() {
7272
c := cache.New(cache.AsLFU[string, int]())
7373
c.Set("a", 1)
7474
c.Set("b", 1)
@@ -78,7 +78,7 @@ func ExampleKeys() {
7878
// [a b c]
7979
}
8080

81-
func ExampleContains() {
81+
func ExampleCache_Contains() {
8282
c := cache.New(cache.AsLRU[string, int]())
8383
c.Set("a", 1)
8484
fmt.Println(c.Contains("a"))
@@ -88,7 +88,7 @@ func ExampleContains() {
8888
// false
8989
}
9090

91-
func ExampleNumberCache() {
91+
func ExampleNewNumber() {
9292
nc := cache.NewNumber[string, int]()
9393
nc.Set("a", 1)
9494
nc.Set("b", 2, cache.WithExpiration(time.Minute))
File renamed without changes.

clock/clock_test.go renamed to policy/clock/clock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"testing"
66

7-
"github.com/Code-Hex/go-generics-cache/clock"
7+
"github.com/Code-Hex/go-generics-cache/policy/clock"
88
)
99

1010
func TestSet(t *testing.T) {

clock/example_test.go renamed to policy/clock/example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package clock_test
33
import (
44
"fmt"
55

6-
"github.com/Code-Hex/go-generics-cache/clock"
6+
"github.com/Code-Hex/go-generics-cache/policy/clock"
77
)
88

9-
func ExampleCache() {
9+
func ExampleNewCache() {
1010
c := clock.NewCache[string, int]()
1111
c.Set("a", 1)
1212
c.Set("b", 2)
@@ -33,7 +33,7 @@ func ExampleCache() {
3333
// 3
3434
}
3535

36-
func ExampleCacheKeys() {
36+
func ExampleCache_Keys() {
3737
c := clock.NewCache[string, int]()
3838
c.Set("foo", 1)
3939
c.Set("bar", 2)

fifo/example_test.go renamed to policy/fifo/example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package fifo_test
33
import (
44
"fmt"
55

6-
"github.com/Code-Hex/go-generics-cache/fifo"
6+
"github.com/Code-Hex/go-generics-cache/policy/fifo"
77
)
88

9-
func ExampleCache() {
9+
func ExampleNewCache() {
1010
c := fifo.NewCache[string, int]()
1111
c.Set("a", 1)
1212
c.Set("b", 2)
@@ -33,7 +33,7 @@ func ExampleCache() {
3333
// 3
3434
}
3535

36-
func ExampleCacheKeys() {
36+
func ExampleCache_Keys() {
3737
c := fifo.NewCache[string, int]()
3838
c.Set("foo", 1)
3939
c.Set("bar", 2)
File renamed without changes.

fifo/fifo_test.go renamed to policy/fifo/fifo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"testing"
66

7-
"github.com/Code-Hex/go-generics-cache/fifo"
7+
"github.com/Code-Hex/go-generics-cache/policy/fifo"
88
)
99

1010
func TestSet(t *testing.T) {

lfu/example_test.go renamed to policy/lfu/example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package lfu_test
33
import (
44
"fmt"
55

6-
"github.com/Code-Hex/go-generics-cache/lfu"
6+
"github.com/Code-Hex/go-generics-cache/policy/lfu"
77
)
88

9-
func ExampleLFUCache() {
9+
func ExampleNewCache() {
1010
c := lfu.NewCache[string, int]()
1111
c.Set("a", 1)
1212
c.Set("b", 2)
@@ -22,7 +22,7 @@ func ExampleLFUCache() {
2222
// 0 false
2323
}
2424

25-
func ExampleCacheKeys() {
25+
func ExampleCache_Keys() {
2626
c := lfu.NewCache[string, int]()
2727
c.Set("a", 1)
2828
c.Set("b", 2)

0 commit comments

Comments
 (0)