Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Cosentino committed Jan 20, 2023
1 parent 3658ea5 commit eb40c06
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 164 deletions.
55 changes: 0 additions & 55 deletions backend/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,59 +75,6 @@ func (cacheBackend *InMemory) Set(item *models.Item) error {
return nil
}

// List the items in the cache that meet the specified criteria.
// func (cacheBackend *InMemory) List(options ...FilterOption[InMemory]) ([]*models.Item, error) {
// // Apply the filter options
// ApplyFilterOptions(cacheBackend, options...)

// items := make([]*models.Item, 0)
// for item := range cacheBackend.items.IterBuffered() {
// if cacheBackend.FilterFunc == nil || cacheBackend.FilterFunc(item.Val) {
// items = append(items, item.Val)
// }
// }

// if cacheBackend.SortBy == "" {
// return items, nil
// }

// sort.Slice(items, func(i, j int) bool {
// a := items[i].FieldByName(cacheBackend.SortBy)
// b := items[j].FieldByName(cacheBackend.SortBy)
// switch cacheBackend.SortBy {
// case types.SortByKey.String():
// if cacheBackend.SortAscending {
// return a.Interface().(string) < b.Interface().(string)
// }
// return a.Interface().(string) > b.Interface().(string)
// case types.SortByValue.String():
// if cacheBackend.SortAscending {
// return a.Interface().(string) < b.Interface().(string)
// }
// return a.Interface().(string) > b.Interface().(string)
// case types.SortByLastAccess.String():
// if cacheBackend.SortAscending {
// return a.Interface().(time.Time).Before(b.Interface().(time.Time))
// }
// return a.Interface().(time.Time).After(b.Interface().(time.Time))
// case types.SortByAccessCount.String():
// if cacheBackend.SortAscending {
// return a.Interface().(uint) < b.Interface().(uint)
// }
// return a.Interface().(uint) > b.Interface().(uint)
// case types.SortByExpiration.String():
// if cacheBackend.SortAscending {
// return a.Interface().(time.Duration) < b.Interface().(time.Duration)
// }
// return a.Interface().(time.Duration) > b.Interface().(time.Duration)
// default:
// return false
// }
// })

// return items, nil
// }

// List returns a list of all items in the cache filtered and ordered by the given options
func (cacheBackend *InMemory) List(options ...FilterOption[InMemory]) ([]*models.Item, error) {
// Apply the filter options
Expand All @@ -148,8 +95,6 @@ func (cacheBackend *InMemory) List(options ...FilterOption[InMemory]) ([]*models
switch cacheBackend.SortBy {
case types.SortByKey.String():
sorter = &itemSorterByKey{items: items}
case types.SortByValue.String():
sorter = &itemSorterByValue{items: items}
case types.SortByLastAccess.String():
sorter = &itemSorterByLastAccess{items: items}
case types.SortByAccessCount.String():
Expand Down
2 changes: 0 additions & 2 deletions backend/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ func (cacheBackend *Redis) List(options ...FilterOption[Redis]) ([]*models.Item,
switch cacheBackend.SortBy {
case types.SortByKey.String(): // Sort by key
sorter = &itemSorterByKey{items: items}
case types.SortByValue.String(): // Sort by value: it can cause problems if the value is not a string
sorter = &itemSorterByValue{items: items}
case types.SortByLastAccess.String(): // Sort by last access
sorter = &itemSorterByLastAccess{items: items}
case types.SortByAccessCount.String(): // Sort by access count
Expand Down
51 changes: 0 additions & 51 deletions backend/sorting.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package backend

import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hyp3rd/hypercache/models"
)

Expand All @@ -26,55 +24,6 @@ func (s *itemSorterByKey) Len() int { return len(s.items) }
func (s *itemSorterByKey) Swap(i, j int) { s.items[i], s.items[j] = s.items[j], s.items[i] }
func (s *itemSorterByKey) Less(i, j int) bool { return s.items[i].Key < s.items[j].Key }

type itemSorterByValue struct {
items []*models.Item
}

// func (s *itemSorterByValue) Len() int { return len(s.items) }
// func (s *itemSorterByValue) Swap(i, j int) { s.items[i], s.items[j] = s.items[j], s.items[i] }
// func (s *itemSorterByValue) Less(i, j int) bool {
// return s.cmp.Equal(s.items[i].Value, s.items[j].Value)
// }

func (s *itemSorterByValue) Len() int { return len(s.items) }
func (s *itemSorterByValue) Swap(i, j int) { s.items[i], s.items[j] = s.items[j], s.items[i] }

// func (s *itemSorterByValue) Less(i, j int) bool { return cmp.Equal(s.items[i].Value, s.items[j].Value) }
func (s *itemSorterByValue) Less(i, j int) bool {
return cmp.Equal(s.items[i].Value, s.items[j].Value,
cmpopts.IgnoreUnexported(),
cmpopts.EquateEmpty(),
cmpopts.SortSlices(func(x, y interface{}) bool {
return x.(string) < y.(string)
},
),
cmpopts.SortMaps(func(x, y interface{}) bool {
return x.(string) < y.(string)
},
),
cmpopts.SortSlices(func(x, y interface{}) bool {
return x.(int) < y.(int)
},
),
cmpopts.SortSlices(func(x, y interface{}) bool {
return x.(string) < y.(string)
},
),
cmpopts.SortMaps(func(x, y interface{}) bool {
return x.(int) < y.(int)
},
),
cmpopts.SortSlices(func(x, y interface{}) bool {
return x.(float64) < y.(float64)
},
),
cmpopts.SortMaps(func(x, y interface{}) bool {
return x.(float64) < y.(float64)
},
),
)
}

type itemSorterByExpiration struct {
items []*models.Item
}
Expand Down
10 changes: 3 additions & 7 deletions examples/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"log"

"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/middleware"
"go.uber.org/zap"
)

func main() {
Expand All @@ -27,17 +27,13 @@ func main() {
}

// Example of using zap logger from uber
logger, _ := zap.NewProduction()

sugar := logger.Sugar()
defer sugar.Sync()
defer logger.Sync()
logger := log.Default()

// apply middleware in the same order as you want to execute them
svc = hypercache.ApplyMiddleware(svc,
// middleware.YourMiddleware,
func(next hypercache.Service) hypercache.Service {
return middleware.NewLoggingMiddleware(next, sugar)
return middleware.NewLoggingMiddleware(next, logger)
},
func(next hypercache.Service) hypercache.Service {
return middleware.NewStatsCollectorMiddleware(next, statsCollector)
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.19

require (
github.com/go-redis/redis/v9 v9.0.0-rc.2
github.com/google/go-cmp v0.5.9
github.com/longbridgeapp/assert v1.1.0
github.com/shamaton/msgpack/v2 v2.1.1
go.uber.org/zap v1.24.0
)

require (
Expand All @@ -17,7 +15,5 @@ require (
github.com/onsi/gomega v1.24.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -10,33 +9,23 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/go-redis/redis/v9 v9.0.0-rc.2 h1:IN1eI8AvJJeWHjMW/hlFAv2sAfvTun2DVksDDJ3a6a0=
github.com/go-redis/redis/v9 v9.0.0-rc.2/go.mod h1:cgBknjwcBJa2prbnuHH/4k/Mlj4r0pWNV2HBanHujfY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/longbridgeapp/assert v1.1.0 h1:L+/HISOhuGbNAAmJNXgk3+Tm5QmSB70kwdktJXgjL+I=
github.com/longbridgeapp/assert v1.1.0/go.mod h1:UOI7O3rzlzlz715lQm0atWs6JbrYGuIJUEeOekutL6o=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.24.2 h1:J/tulyYK6JwBldPViHJReihxxZ+22FHs0piGjQAvoUE=
github.com/onsi/gomega v1.24.2/go.mod h1:gs3J10IS7Z7r7eXRoNJIrNqU4ToQukCJhFtKrWgHWnk=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shamaton/msgpack/v2 v2.1.1 h1:gAMxOtVJz93R0EwewwUc8tx30n34aV6BzJuwHE8ogAk=
github.com/shamaton/msgpack/v2 v2.1.1/go.mod h1:aTUEmh31ziGX1Ml7wMPLVY0f4vT3CRsCvZRoSCs+VGg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
Expand Down
10 changes: 2 additions & 8 deletions hypercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package hypercache
// It can implement a service interface to interact with the cache with middleware support (default or custom).

import (
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -259,6 +258,7 @@ func (hyperCache *HyperCache[T]) evictionLoop() {
if hyperCache.maxEvictionCount == uint(evictedCount) {
break
}

key, ok := hyperCache.evictionAlgorithm.Evict()

if !ok {
Expand All @@ -280,17 +280,12 @@ func (hyperCache *HyperCache[T]) evictionLoop() {
func (hyperCache *HyperCache[T]) evictItem() (string, bool) {
key, ok := hyperCache.evictionAlgorithm.Evict()
if !ok {
fmt.Println("failed evicting item: ", key)

// no more items to evict
return "", false
}

fmt.Println("evicting item: ", key)

err := hyperCache.backend.Remove(key)
if err != nil {
fmt.Println("failed evicting item: ", key, err)

return "", false
}
return key, true
Expand Down Expand Up @@ -380,7 +375,6 @@ func (hyperCache *HyperCache[T]) SetMultiple(items map[string]any, expiration ti
func (hyperCache *HyperCache[T]) Get(key string) (value any, ok bool) {
item, ok := hyperCache.backend.Get(key)
if !ok {
fmt.Println("not found")
return nil, false
}

Expand Down
Loading

0 comments on commit eb40c06

Please sign in to comment.