@@ -42,9 +42,9 @@ import (
4242
4343const satoriCacheCleanupInterval = 5 * time .Second
4444
45- type satoriCache [T any ] interface {
46- Get (ctx context.Context , userID string , names , labels []string ) (values []T , missingNames , missingLabels []string )
47- Add (ctx context.Context , userID string , names , labels []string , values map [string ]T )
45+ type satoriCache [T any , O comparable ] interface {
46+ Get (ctx context.Context , userID string , names , labels []string , optFilters ... O ) (values []T , missingNames , missingLabels []string )
47+ Add (ctx context.Context , userID string , names , labels []string , values map [string ]T , optFilters ... O )
4848 SetAll (ctx context.Context , userID string , values map [string ]T )
4949}
5050
@@ -66,10 +66,10 @@ type SatoriClient struct {
6666 cacheEnabled bool
6767 propertiesCacheMutex sync.RWMutex
6868 propertiesCache map [context.Context ]* runtime.Properties
69- flagsCache satoriCache [flagCacheEntry ]
70- flagsOverridesCache satoriCache [flagOverridesCacheEntry ]
71- liveEventsCache satoriCache [* runtime.LiveEvent ]
72- experimentsCache satoriCache [* runtime.Experiment ]
69+ flagsCache satoriCache [flagCacheEntry , struct {} ]
70+ flagsOverridesCache satoriCache [flagOverridesCacheEntry , struct {} ]
71+ liveEventsCache satoriCache [* runtime.LiveEvent , liveEventFilters ]
72+ experimentsCache satoriCache [* runtime.Experiment , struct {} ]
7373}
7474
7575func NewSatoriClient (ctx context.Context , logger * zap.Logger , satoriUrl , apiKeyName , apiKey , serverKey , signingKey string , nakamaTokenExpirySec , httpTimeoutMs int64 , cacheEnabled bool , cacheMode string , cacheTTLSec int64 ) * SatoriClient {
@@ -92,25 +92,25 @@ func NewSatoriClient(ctx context.Context, logger *zap.Logger, satoriUrl, apiKeyN
9292 cacheEnabled : cacheEnabled ,
9393 propertiesCacheMutex : sync.RWMutex {},
9494 propertiesCache : make (map [context.Context ]* runtime.Properties ),
95- flagsCache : newSatoriContextCache [flagCacheEntry ](ctx , cacheEnabled ),
96- flagsOverridesCache : newSatoriContextCache [flagOverridesCacheEntry ](ctx , cacheEnabled ),
97- liveEventsCache : newSatoriContextCache [* runtime.LiveEvent ](ctx , cacheEnabled ),
98- experimentsCache : newSatoriContextCache [* runtime.Experiment ](ctx , cacheEnabled ),
95+ flagsCache : newSatoriContextCache [flagCacheEntry , struct {} ](ctx , cacheEnabled ),
96+ flagsOverridesCache : newSatoriContextCache [flagOverridesCacheEntry , struct {} ](ctx , cacheEnabled ),
97+ liveEventsCache : newSatoriContextCache [* runtime.LiveEvent , liveEventFilters ](ctx , cacheEnabled ),
98+ experimentsCache : newSatoriContextCache [* runtime.Experiment , struct {} ](ctx , cacheEnabled ),
9999 }
100100
101101 switch cacheMode {
102102 case "time" :
103- sc .flagsCache = newSatoriTimeCache [flagCacheEntry ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
104- sc .flagsOverridesCache = newSatoriTimeCache [flagOverridesCacheEntry ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
105- sc .liveEventsCache = newSatoriTimeCache [* runtime.LiveEvent ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
106- sc .experimentsCache = newSatoriTimeCache [* runtime.Experiment ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
103+ sc .flagsCache = newSatoriTimeCache [flagCacheEntry , struct {} ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
104+ sc .flagsOverridesCache = newSatoriTimeCache [flagOverridesCacheEntry , struct {} ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
105+ sc .liveEventsCache = newSatoriTimeCache [* runtime.LiveEvent , liveEventFilters ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
106+ sc .experimentsCache = newSatoriTimeCache [* runtime.Experiment , struct {} ](ctx , cacheEnabled , time .Duration (cacheTTLSec )* time .Second )
107107 case "context" :
108108 fallthrough
109109 default :
110- sc .flagsCache = newSatoriContextCache [flagCacheEntry ](ctx , cacheEnabled )
111- sc .flagsOverridesCache = newSatoriContextCache [flagOverridesCacheEntry ](ctx , cacheEnabled )
112- sc .liveEventsCache = newSatoriContextCache [* runtime.LiveEvent ](ctx , cacheEnabled )
113- sc .experimentsCache = newSatoriContextCache [* runtime.Experiment ](ctx , cacheEnabled )
110+ sc .flagsCache = newSatoriContextCache [flagCacheEntry , struct {} ](ctx , cacheEnabled )
111+ sc .flagsOverridesCache = newSatoriContextCache [flagOverridesCacheEntry , struct {} ](ctx , cacheEnabled )
112+ sc .liveEventsCache = newSatoriContextCache [* runtime.LiveEvent , liveEventFilters ](ctx , cacheEnabled )
113+ sc .experimentsCache = newSatoriContextCache [* runtime.Experiment , struct {} ](ctx , cacheEnabled )
114114 }
115115
116116 if sc .urlString == "" && sc .apiKeyName == "" && sc .apiKey == "" && sc .signingKey == "" {
@@ -947,7 +947,14 @@ func (s *SatoriClient) LiveEventsList(ctx context.Context, id string, names, lab
947947 return nil , status .Errorf (codes .InvalidArgument , "start_time_sec and end_time_sec must be greater than 0" )
948948 }
949949
950- entry , missingNames , missingLabels := s .liveEventsCache .Get (ctx , id , names , labels )
950+ optFilters := liveEventFilters {
951+ pastRunCount : pastRunCount ,
952+ futureRunCount : futureRunCount ,
953+ startTimeSec : startTimeSec ,
954+ endTimeSec : endTimeSec ,
955+ }
956+
957+ entry , missingNames , missingLabels := s .liveEventsCache .Get (ctx , id , names , labels , optFilters )
951958
952959 if ! s .cacheEnabled || entry == nil || len (missingNames ) > 0 || len (missingLabels ) > 0 {
953960 url := s .url .JoinPath ("/v1/live-event" ).String ()
@@ -1015,11 +1022,7 @@ func (s *SatoriClient) LiveEventsList(ctx context.Context, id string, names, lab
10151022 entry = append (entry , le )
10161023 }
10171024
1018- if len (names ) > 0 {
1019- s .liveEventsCache .Add (ctx , id , names , labels , entries )
1020- } else {
1021- s .liveEventsCache .SetAll (ctx , id , entries )
1022- }
1025+ s .liveEventsCache .Add (ctx , id , names , labels , entries , optFilters )
10231026
10241027 return liveEvents , nil
10251028 default :
0 commit comments