-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.go
56 lines (44 loc) · 1.44 KB
/
plugin.go
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 featureflags
import (
"sync"
"github.com/mrbentarikau/pagst/common"
"github.com/mrbentarikau/pagst/common/pubsub"
)
var logger = common.GetPluginLogger(&Plugin{})
// Plugin represents the mqueue plugin
type Plugin struct {
stopBGWorker chan *sync.WaitGroup
}
// PluginInfo implements common.Plugin
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "featureflags",
SysName: "featureflags",
Category: common.PluginCategoryCore,
}
}
type EvictCacheData struct {
GuildID int64 `json:"guild_id"`
}
// RegisterPlugin registers the mqueue plugin into the plugin system and also initializes it
func RegisterPlugin() {
p := &Plugin{
stopBGWorker: make(chan *sync.WaitGroup),
}
common.RegisterPlugin(p)
pubsub.AddHandler(evictCachePubSubEvent, handleInvalidateCacheFor, nil)
pubsub.AddHandler(evictCachePubSubEvent2, handleInvalidateCacheFor2, EvictCacheData{})
}
// Invalidate the cache when the rules have changed
func handleInvalidateCacheFor(event *pubsub.Event) {
EvictCacheForGuild(event.TargetGuildInt)
}
func handleInvalidateCacheFor2(event *pubsub.Event) {
dataCast := event.Data.(*EvictCacheData)
EvictCacheForGuild(dataCast.GuildID)
}
// EvictCacheForGuild evicts the specified guild's featureflag cache
func EvictCacheForGuild(guildID int64) {
cacheID := (guildID >> 22) % int64(len(caches))
caches[cacheID].invalidateGuild(guildID)
}