-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
62 lines (55 loc) · 2.28 KB
/
service.go
File metadata and controls
62 lines (55 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hypercache
import (
"context"
"time"
"github.com/hyp3rd/hypercache/pkg/backend"
cache "github.com/hyp3rd/hypercache/pkg/cache/v2"
"github.com/hyp3rd/hypercache/pkg/stats"
)
// Service is the service interface for the HyperCache.
// It enables middleware to be added to the service.
type Service interface {
crud
// Capacity returns the capacity of the cache
Capacity() int
// Allocation returns the allocation in bytes of the current cache
Allocation() int64
// Count returns the number of items in the cache
Count(ctx context.Context) int
// TriggerEviction triggers the eviction of the cache
TriggerEviction(ctx context.Context)
// Stop stops the cache
Stop(ctx context.Context) error
// GetStats returns the stats of the cache
GetStats() stats.Stats
}
type crud interface {
// Get retrieves a value from the cache using the key
Get(ctx context.Context, key string) (value any, ok bool)
// Set stores a value in the cache using the key and expiration duration
Set(ctx context.Context, key string, value any, expiration time.Duration) error
// GetOrSet retrieves a value from the cache using the key, if the key does not exist, it will set the value using the key and
// expiration duration
GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error)
// GetWithInfo fetches from the cache using the key, and returns the `cache.Item` and a boolean indicating if the key exists
GetWithInfo(ctx context.Context, key string) (*cache.Item, bool)
// GetMultiple retrieves a list of values from the cache using the keys
GetMultiple(ctx context.Context, keys ...string) (result map[string]any, failed map[string]error)
// List returns a list of all items in the cache
List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error)
// Remove removes a value from the cache using the key
Remove(ctx context.Context, keys ...string) error
// Clear removes all values from the cache
Clear(ctx context.Context) error
}
// Middleware describes a service middleware.
type Middleware func(Service) Service
// ApplyMiddleware applies middlewares to a service.
func ApplyMiddleware(svc Service, mw ...Middleware) Service {
// Apply each middleware in the chain
for _, m := range mw {
svc = m(svc)
}
// Return the decorated service
return svc
}