Skip to content

Commit 5b6d824

Browse files
committed
refactor: restructure cache types into pkg/cache package
Move cache-related types and utilities from scattered locations into a centralized pkg/cache package structure following Go package conventions. Package changes: - Move types/item.go to pkg/cache/item.go - Move cache/cmap.go to pkg/cache/cmap.go - Move cache/v4/cmap.go to pkg/cache/v4/cmap.go Type updates: - types.Item → cache.Item - types.ItemPoolManager → cache.ItemPoolManager - Update all imports and references throughout codebase This reorganization improves package clarity by grouping all cache-related types under a single, well-defined package hierarchy. The pkg/ directory follows Go conventions for reusable packages, while maintaining all existing functionality and APIs. Affected components: backends, eviction algorithms, middleware, examples, and tests - all updated to use the new package structure.
1 parent 2982bc4 commit 5b6d824

17 files changed

Lines changed: 86 additions & 81 deletions

File tree

backend/backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package backend
33
import (
44
"context"
55

6-
"github.com/hyp3rd/hypercache/types"
6+
"github.com/hyp3rd/hypercache/pkg/cache"
77
)
88

99
// IBackendConstrain is the interface that defines the constrain type implemented by cache backends.
@@ -15,9 +15,9 @@ type IBackendConstrain interface {
1515
type IBackend[T IBackendConstrain] interface {
1616
// Get retrieves the item with the given key from the cache.
1717
// If the key is not found in the cache, it returns nil.
18-
Get(ctx context.Context, key string) (item *types.Item, ok bool)
18+
Get(ctx context.Context, key string) (item *cache.Item, ok bool)
1919
// Set adds a new item to the cache.
20-
Set(ctx context.Context, item *types.Item) error
20+
Set(ctx context.Context, item *cache.Item) error
2121
// Capacity returns the maximum number of items that can be stored in the cache.
2222
Capacity() int
2323
// SetCapacity sets the maximum number of items that can be stored in the cache.
@@ -27,7 +27,7 @@ type IBackend[T IBackendConstrain] interface {
2727
// Remove deletes the item with the given key from the cache.
2828
Remove(ctx context.Context, keys ...string) error
2929
// List the items in the cache that meet the specified criteria.
30-
List(ctx context.Context, filters ...IFilter) (items []*types.Item, err error)
30+
List(ctx context.Context, filters ...IFilter) (items []*cache.Item, err error)
3131
// Clear removes all items from the cache.
3232
Clear(ctx context.Context) error
3333
}

backend/filters.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55

66
"github.com/hyp3rd/ewrap"
77

8+
"github.com/hyp3rd/hypercache/pkg/cache"
89
"github.com/hyp3rd/hypercache/types"
910
)
1011

1112
// itemSorter is a custom sorter for the items.
1213
type itemSorter struct {
13-
items []*types.Item
14-
less func(i, j *types.Item) bool
14+
items []*cache.Item
15+
less func(i, j *cache.Item) bool
1516
}
1617

1718
func (s *itemSorter) Len() int { return len(s.items) }
@@ -20,7 +21,7 @@ func (s *itemSorter) Less(i, j int) bool { return s.less(s.items[i], s.items[j])
2021

2122
// IFilter is a backend agnostic interface for a filter that can be applied to a list of items.
2223
type IFilter interface {
23-
ApplyFilter(backendType string, items []*types.Item) ([]*types.Item, error)
24+
ApplyFilter(backendType string, items []*cache.Item) ([]*cache.Item, error)
2425
}
2526

2627
// sortByFilter is a filter that sorts the items by a given field.
@@ -35,7 +36,7 @@ type SortOrderFilter struct {
3536

3637
// filterFunc is a filter that filters the items by a given field's value.
3738
type filterFunc struct {
38-
fn func(item *types.Item) bool
39+
fn func(item *cache.Item) bool
3940
}
4041

4142
// WithSortBy returns a filter that sorts the items by a given field.
@@ -49,40 +50,40 @@ func WithSortOrderAsc(ascending bool) SortOrderFilter {
4950
}
5051

5152
// WithFilterFunc returns a filter that filters the items by a given field's value.
52-
func WithFilterFunc(fn func(item *types.Item) bool) IFilter {
53+
func WithFilterFunc(fn func(item *cache.Item) bool) IFilter {
5354
return filterFunc{fn: fn}
5455
}
5556

5657
// ApplyFilter applies the sort by filter to the given list of items.
57-
func (f sortByFilter) ApplyFilter(_ string, items []*types.Item) ([]*types.Item, error) {
58+
func (f sortByFilter) ApplyFilter(_ string, items []*cache.Item) ([]*cache.Item, error) {
5859
var sorter *itemSorter
5960

6061
switch f.field {
6162
case types.SortByKey.String():
6263
sorter = &itemSorter{
6364
items: items,
64-
less: func(i, j *types.Item) bool {
65+
less: func(i, j *cache.Item) bool {
6566
return i.Key < j.Key
6667
},
6768
}
6869
case types.SortByLastAccess.String():
6970
sorter = &itemSorter{
7071
items: items,
71-
less: func(i, j *types.Item) bool {
72+
less: func(i, j *cache.Item) bool {
7273
return i.LastAccess.UnixNano() < j.LastAccess.UnixNano()
7374
},
7475
}
7576
case types.SortByAccessCount.String():
7677
sorter = &itemSorter{
7778
items: items,
78-
less: func(i, j *types.Item) bool {
79+
less: func(i, j *cache.Item) bool {
7980
return i.AccessCount < j.AccessCount
8081
},
8182
}
8283
case types.SortByExpiration.String():
8384
sorter = &itemSorter{
8485
items: items,
85-
less: func(i, j *types.Item) bool {
86+
less: func(i, j *cache.Item) bool {
8687
return i.Expiration < j.Expiration
8788
},
8889
}
@@ -96,7 +97,7 @@ func (f sortByFilter) ApplyFilter(_ string, items []*types.Item) ([]*types.Item,
9697
}
9798

9899
// ApplyFilter applies the sort order filter to the given list of items.
99-
func (f SortOrderFilter) ApplyFilter(_ string, items []*types.Item) ([]*types.Item, error) {
100+
func (f SortOrderFilter) ApplyFilter(_ string, items []*cache.Item) ([]*cache.Item, error) {
100101
if !f.ascending {
101102
for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
102103
items[i], items[j] = items[j], items[i]
@@ -107,8 +108,8 @@ func (f SortOrderFilter) ApplyFilter(_ string, items []*types.Item) ([]*types.It
107108
}
108109

109110
// ApplyFilter applies the filter function to the given list of items.
110-
func (f filterFunc) ApplyFilter(_ string, items []*types.Item) ([]*types.Item, error) {
111-
filteredItems := make([]*types.Item, 0)
111+
func (f filterFunc) ApplyFilter(_ string, items []*cache.Item) ([]*cache.Item, error) {
112+
filteredItems := make([]*cache.Item, 0)
112113

113114
for _, item := range items {
114115
if f.fn(item) {

backend/inmemory.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import (
44
"context"
55
"sync"
66

7-
cache "github.com/hyp3rd/hypercache/cache/v4"
87
"github.com/hyp3rd/hypercache/internal/constants"
98
"github.com/hyp3rd/hypercache/internal/sentinel"
10-
"github.com/hyp3rd/hypercache/types"
9+
"github.com/hyp3rd/hypercache/pkg/cache"
10+
cachev4 "github.com/hyp3rd/hypercache/pkg/cache/v4"
1111
)
1212

1313
// InMemory is a cache backend that stores the items in memory, leveraging a custom `ConcurrentMap`.
1414
type InMemory struct {
1515
sync.RWMutex // mutex to protect the cache from concurrent access
1616

17-
items cache.ConcurrentMap // map to store the items in the cache
18-
itemPoolManager *types.ItemPoolManager // item pool manager to manage the item pool
17+
items cachev4.ConcurrentMap // map to store the items in the cache
18+
itemPoolManager *cache.ItemPoolManager // item pool manager to manage the item pool
1919
capacity int // capacity of the cache, limits the number of items that can be stored in the cache
2020
}
2121

2222
// NewInMemory creates a new in-memory cache with the given options.
2323
func NewInMemory(opts ...Option[InMemory]) (IBackend[InMemory], error) {
2424
InMemory := &InMemory{
25-
items: cache.New(),
26-
itemPoolManager: types.NewItemPoolManager(),
25+
items: cachev4.New(),
26+
itemPoolManager: cache.NewItemPoolManager(),
2727
}
2828
// Apply the backend options
2929
ApplyOptions(InMemory, opts...)
@@ -55,7 +55,7 @@ func (cacheBackend *InMemory) Count(_ context.Context) int {
5555
}
5656

5757
// Get retrieves the item with the given key from the cacheBackend. If the item is not found, it returns nil.
58-
func (cacheBackend *InMemory) Get(_ context.Context, key string) (*types.Item, bool) {
58+
func (cacheBackend *InMemory) Get(_ context.Context, key string) (*cache.Item, bool) {
5959
item, ok := cacheBackend.items.Get(key)
6060
if !ok {
6161
return nil, false
@@ -65,7 +65,7 @@ func (cacheBackend *InMemory) Get(_ context.Context, key string) (*types.Item, b
6565
}
6666

6767
// Set adds a Item to the cache.
68-
func (cacheBackend *InMemory) Set(_ context.Context, item *types.Item) error {
68+
func (cacheBackend *InMemory) Set(_ context.Context, item *cache.Item) error {
6969
// Check for invalid key, value, or duration
7070
err := item.Valid()
7171
if err != nil {
@@ -83,14 +83,14 @@ func (cacheBackend *InMemory) Set(_ context.Context, item *types.Item) error {
8383
}
8484

8585
// List returns a list of all items in the cache filtered and ordered by the given options.
86-
func (cacheBackend *InMemory) List(_ context.Context, filters ...IFilter) ([]*types.Item, error) {
86+
func (cacheBackend *InMemory) List(_ context.Context, filters ...IFilter) ([]*cache.Item, error) {
8787
// Apply the filters
8888
cacheBackend.RLock()
8989
defer cacheBackend.RUnlock()
9090

9191
var err error
9292

93-
items := make([]*types.Item, 0, cacheBackend.items.Count())
93+
items := make([]*cache.Item, 0, cacheBackend.items.Count())
9494

9595
for item := range cacheBackend.items.IterBuffered() {
9696
cloned := item

backend/redis.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/hyp3rd/hypercache/internal/constants"
1313
"github.com/hyp3rd/hypercache/internal/sentinel"
1414
"github.com/hyp3rd/hypercache/libs/serializer"
15-
"github.com/hyp3rd/hypercache/types"
15+
"github.com/hyp3rd/hypercache/pkg/cache"
1616
)
1717

1818
const (
@@ -24,15 +24,15 @@ const (
2424
type Redis struct {
2525
rdb *redis.Client // redis client to interact with the redis server
2626
capacity int // capacity of the cache, limits the number of items that can be stored in the cache
27-
itemPoolManager *types.ItemPoolManager // itemPoolManager is used to manage the item pool for memory efficiency
27+
itemPoolManager *cache.ItemPoolManager // itemPoolManager is used to manage the item pool for memory efficiency
2828
keysSetName string // keysSetName is the name of the set that holds the keys of the items in the cache
2929
Serializer serializer.ISerializer // Serializer is the serializer used to serialize the items before storing them in the cache
3030
}
3131

3232
// NewRedis creates a new redis cache with the given options.
3333
func NewRedis(redisOptions ...Option[Redis]) (IBackend[Redis], error) {
3434
rb := &Redis{
35-
itemPoolManager: types.NewItemPoolManager(),
35+
itemPoolManager: cache.NewItemPoolManager(),
3636
}
3737
// Apply the backend options
3838
ApplyOptions(rb, redisOptions...)
@@ -89,7 +89,7 @@ func (cacheBackend *Redis) Count(ctx context.Context) int {
8989
}
9090

9191
// Get retrieves the Item with the given key from the cacheBackend. If the item is not found, it returns nil.
92-
func (cacheBackend *Redis) Get(ctx context.Context, key string) (*types.Item, bool) {
92+
func (cacheBackend *Redis) Get(ctx context.Context, key string) (*cache.Item, bool) {
9393
// Check if the key is in the set of keys
9494
isMember, err := cacheBackend.rdb.SIsMember(ctx, cacheBackend.keysSetName, key).Result()
9595
if err != nil {
@@ -124,7 +124,7 @@ func (cacheBackend *Redis) Get(ctx context.Context, key string) (*types.Item, bo
124124
}
125125

126126
// Set stores the Item in the cacheBackend.
127-
func (cacheBackend *Redis) Set(ctx context.Context, item *types.Item) error {
127+
func (cacheBackend *Redis) Set(ctx context.Context, item *cache.Item) error {
128128
pipe := cacheBackend.rdb.TxPipeline()
129129

130130
// Check if the item is valid
@@ -167,7 +167,7 @@ func (cacheBackend *Redis) Set(ctx context.Context, item *types.Item) error {
167167
}
168168

169169
// List returns a list of all the items in the cacheBackend that match the given filter options.
170-
func (cacheBackend *Redis) List(ctx context.Context, filters ...IFilter) ([]*types.Item, error) {
170+
func (cacheBackend *Redis) List(ctx context.Context, filters ...IFilter) ([]*cache.Item, error) {
171171
// Get the set of keys held in the cacheBackend with the given `keysSetName`
172172
keys, err := cacheBackend.rdb.SMembers(ctx, cacheBackend.keysSetName).Result()
173173
if err != nil {
@@ -188,7 +188,7 @@ func (cacheBackend *Redis) List(ctx context.Context, filters ...IFilter) ([]*typ
188188
}
189189

190190
// Create a slice to hold the items
191-
items := make([]*types.Item, 0, len(keys))
191+
items := make([]*cache.Item, 0, len(keys))
192192

193193
// Deserialize the items and add them to the slice of items to return
194194
for _, cmd := range cmds {

cspell.config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ words:
66
- benchmem
77
- benchtime
88
- cacheerrors
9+
- cachev
910
- CAWOLFU
1011
- Cbor
1112
- chans
@@ -50,6 +51,7 @@ words:
5051
- staticcheck
5152
- stdlib
5253
- strfnv
54+
- ugorji
5355
- varnamelen
5456
- wrapcheck
5557
- Wrapf

eviction/arc.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
"sync"
1111

1212
"github.com/hyp3rd/hypercache/internal/sentinel"
13-
"github.com/hyp3rd/hypercache/types"
13+
"github.com/hyp3rd/hypercache/pkg/cache"
1414
)
1515

1616
// ARC is an in-memory cache that uses the Adaptive Replacement Cache (ARC) algorithm to manage its items.
1717
type ARC struct {
18-
itemPoolManager *types.ItemPoolManager // itemPoolManager is used to manage the item pool for memory efficiency
18+
itemPoolManager *cache.ItemPoolManager // itemPoolManager is used to manage the item pool for memory efficiency
1919
capacity int // capacity is the maximum number of items that can be stored in the cache
20-
t1 map[string]*types.Item // t1 is a list of items that have been accessed recently
21-
t2 map[string]*types.Item // t2 is a list of items that have been accessed less recently
20+
t1 map[string]*cache.Item // t1 is a list of items that have been accessed recently
21+
t2 map[string]*cache.Item // t2 is a list of items that have been accessed less recently
2222
b1 map[string]bool // b1 is a list of items that have been evicted from t1
2323
b2 map[string]bool // b2 is a list of items that have been evicted from t2
2424
p int // p is the promotion threshold
@@ -34,10 +34,10 @@ func NewARCAlgorithm(capacity int) (*ARC, error) {
3434
}
3535

3636
return &ARC{
37-
itemPoolManager: types.NewItemPoolManager(),
37+
itemPoolManager: cache.NewItemPoolManager(),
3838
capacity: capacity,
39-
t1: make(map[string]*types.Item, capacity),
40-
t2: make(map[string]*types.Item, capacity),
39+
t1: make(map[string]*cache.Item, capacity),
40+
t2: make(map[string]*cache.Item, capacity),
4141
b1: make(map[string]bool, capacity),
4242
b2: make(map[string]bool, capacity),
4343
p: 0,

eviction/cawolfu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package eviction
55
import (
66
"sync"
77

8-
"github.com/hyp3rd/hypercache/cache"
98
"github.com/hyp3rd/hypercache/internal/sentinel"
9+
"github.com/hyp3rd/hypercache/pkg/cache"
1010
)
1111

1212
// CAWOLFU is an eviction algorithm that uses the Cache-Aware Write-Optimized LFU (CAWOLFU) policy to select items for eviction.

eviction/clock.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"sync"
1111

1212
"github.com/hyp3rd/hypercache/internal/sentinel"
13-
"github.com/hyp3rd/hypercache/types"
13+
"github.com/hyp3rd/hypercache/pkg/cache"
1414
)
1515

1616
// ClockAlgorithm is an in-memory cache with the Clock algorithm.
1717
type ClockAlgorithm struct {
18-
items []*types.Item
19-
itemPoolManager *types.ItemPoolManager
18+
items []*cache.Item
19+
itemPoolManager *cache.ItemPoolManager
2020
keys map[string]int
2121
mutex sync.RWMutex
2222
evictMutex sync.Mutex
@@ -31,8 +31,8 @@ func NewClockAlgorithm(capacity int) (*ClockAlgorithm, error) {
3131
}
3232

3333
return &ClockAlgorithm{
34-
items: make([]*types.Item, capacity),
35-
itemPoolManager: types.NewItemPoolManager(),
34+
items: make([]*cache.Item, capacity),
35+
itemPoolManager: cache.NewItemPoolManager(),
3636
keys: make(map[string]int, capacity),
3737
capacity: capacity,
3838
hand: 0,

examples/list/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hyp3rd/hypercache"
1111
"github.com/hyp3rd/hypercache/backend"
12+
"github.com/hyp3rd/hypercache/pkg/cache"
1213
"github.com/hyp3rd/hypercache/types"
1314
)
1415

@@ -47,7 +48,7 @@ func main() {
4748

4849
// Apply filters
4950
// Define a filter function
50-
itemsFilterFunc := func(item *types.Item) bool {
51+
itemsFilterFunc := func(item *cache.Item) bool {
5152
// return time.Since(item.LastAccess) > 1*time.Microsecond
5253
return item.Value != "val8"
5354
}

examples/redis/redis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hyp3rd/hypercache/backend"
1111
"github.com/hyp3rd/hypercache/backend/redis"
1212
"github.com/hyp3rd/hypercache/internal/constants"
13+
"github.com/hyp3rd/hypercache/pkg/cache"
1314
"github.com/hyp3rd/hypercache/types"
1415
)
1516

@@ -62,7 +63,7 @@ func main() {
6263

6364
// Apply filters
6465
// Define a filter function
65-
itemsFilterFunc := func(item *types.Item) bool {
66+
itemsFilterFunc := func(item *cache.Item) bool {
6667
// return time.Since(item.LastAccess) > 1*time.Microsecond
6768
return item.Value != "value-16"
6869
}

0 commit comments

Comments
 (0)