-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions_cache.go
More file actions
56 lines (46 loc) · 1.32 KB
/
options_cache.go
File metadata and controls
56 lines (46 loc) · 1.32 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
package frame
import (
"context"
"github.com/pitabwire/frame/cache"
)
// WithCacheManager adds a cache manager to the service.
func WithCacheManager() Option {
return func(_ context.Context, s *Service) {
s.registerPlugin("cache")
if s.cacheManager == nil {
s.cacheManager = cache.NewManager()
// Register cleanup method
s.AddCleanupMethod(func(_ context.Context) {
if s.cacheManager != nil {
_ = s.cacheManager.Close()
}
})
}
}
}
// WithCache adds a raw cache with the given name to the service.
func WithCache(name string, rawCache cache.RawCache) Option {
return func(ctx context.Context, s *Service) {
// Ensure cache manager is initialized
if s.cacheManager == nil {
WithCacheManager()(ctx, s)
}
// Add cache
s.cacheManager.AddCache(name, rawCache)
}
}
// WithInMemoryCache adds an in-memory cache with the given name.
func WithInMemoryCache(name string) Option {
return WithCache(name, cache.NewInMemoryCache())
}
// CacheManager returns the service's cache manager.
func (s *Service) CacheManager() cache.Manager {
return s.cacheManager
}
// GetRawCache is a convenience method to get a raw cache by name from the service.
func (s *Service) GetRawCache(name string) (cache.RawCache, bool) {
if s.cacheManager == nil {
return nil, false
}
return s.cacheManager.GetRawCache(name)
}