-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.go
More file actions
68 lines (55 loc) · 1.65 KB
/
pubsub.go
File metadata and controls
68 lines (55 loc) · 1.65 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
63
64
65
66
67
68
package cachegrid
import (
"github.com/skshohagmiah/cachegrid/internal/pubsub"
)
// EventType re-exports the internal event type.
type EventType = pubsub.EventType
// Event type constants.
const (
EventSet = pubsub.EventSet
EventDelete = pubsub.EventDelete
EventExpire = pubsub.EventExpire
EventEvict = pubsub.EventEvict
)
// CacheEvent is a public cache event.
type CacheEvent = pubsub.Event
// Subscription wraps an internal subscription.
type Subscription struct {
internal *pubsub.Subscription
}
// Events returns the channel to receive events on.
func (s *Subscription) Events() <-chan CacheEvent {
return s.internal.Events()
}
// Close unsubscribes and closes the event channel.
func (s *Subscription) Close() {
s.internal.Close()
}
// Subscribe returns a subscription for events matching the glob pattern.
func (c *Cache) Subscribe(pattern string) *Subscription {
return &Subscription{internal: c.broker.Subscribe(pattern)}
}
// OnHit registers a callback for cache hits.
func (c *Cache) OnHit(fn func(key string)) {
c.broker.OnHit(fn)
}
// OnMiss registers a callback for cache misses.
func (c *Cache) OnMiss(fn func(key string)) {
c.broker.OnMiss(fn)
}
// OnEvict registers a callback for evictions.
func (c *Cache) OnEvict(fn func(key string, value interface{})) {
c.broker.OnEvict(func(key string, value []byte) {
fn(key, value)
})
}
// OnSet registers a callback for set operations.
func (c *Cache) OnSet(fn func(key string, value interface{})) {
c.broker.OnSet(func(key string, value []byte) {
fn(key, value)
})
}
// OnDelete registers a callback for delete operations.
func (c *Cache) OnDelete(fn func(key string)) {
c.broker.OnDelete(fn)
}