-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package statestore | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
cache "github.com/Code-Hex/go-generics-cache" | ||
"open-match.dev/open-match/pkg/pb" | ||
) | ||
|
||
type ticketCacheOptions struct { | ||
ttl time.Duration | ||
} | ||
|
||
func defaultTicketCacheOptions() *ticketCacheOptions { | ||
return &ticketCacheOptions{ | ||
ttl: 10 * time.Second, | ||
} | ||
} | ||
|
||
type TicketCacheOption interface { | ||
apply(options *ticketCacheOptions) | ||
} | ||
|
||
type ticketCacheOptionFunc func(options *ticketCacheOptions) | ||
|
||
func (f ticketCacheOptionFunc) apply(options *ticketCacheOptions) { | ||
f(options) | ||
} | ||
|
||
func WithTicketCacheTTL(ttl time.Duration) TicketCacheOption { | ||
return ticketCacheOptionFunc(func(options *ticketCacheOptions) { | ||
options.ttl = ttl | ||
}) | ||
} | ||
|
||
// StoreWithTicketCache caches GetTicket results in-memory with TTL | ||
type StoreWithTicketCache struct { | ||
origin StateStore | ||
ticketCache *cache.Cache[string, *pb.Ticket] | ||
options *ticketCacheOptions | ||
} | ||
|
||
func NewStoreWithTicketCache(origin StateStore, ticketCache *cache.Cache[string, *pb.Ticket], opts ...TicketCacheOption) *StoreWithTicketCache { | ||
options := defaultTicketCacheOptions() | ||
for _, o := range opts { | ||
o.apply(options) | ||
} | ||
return &StoreWithTicketCache{ | ||
origin: origin, | ||
ticketCache: ticketCache, | ||
options: options, | ||
} | ||
} | ||
|
||
func (s *StoreWithTicketCache) CreateTicket(ctx context.Context, ticket *pb.Ticket) error { | ||
return s.origin.CreateTicket(ctx, ticket) | ||
} | ||
|
||
func (s *StoreWithTicketCache) DeleteTicket(ctx context.Context, ticketID string) error { | ||
s.ticketCache.Delete(ticketID) | ||
return s.origin.DeleteTicket(ctx, ticketID) | ||
} | ||
|
||
func (s *StoreWithTicketCache) GetTicket(ctx context.Context, ticketID string) (*pb.Ticket, error) { | ||
if ticket, hit := s.ticketCache.Get(ticketID); hit { | ||
return ticket, nil | ||
} | ||
ticket, err := s.origin.GetTicket(ctx, ticketID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.ticketCache.Set(ticketID, ticket, cache.WithExpiration(s.options.ttl)) | ||
return ticket, nil | ||
} | ||
|
||
func (s *StoreWithTicketCache) GetAssignment(ctx context.Context, ticketID string) (*pb.Assignment, error) { | ||
return s.origin.GetAssignment(ctx, ticketID) | ||
} | ||
|
||
func (s *StoreWithTicketCache) GetActiveTickets(ctx context.Context, limit int64) ([]*pb.Ticket, error) { | ||
return s.origin.GetActiveTickets(ctx, limit) | ||
} | ||
|
||
func (s *StoreWithTicketCache) ReleaseTickets(ctx context.Context, ticketIDs []string) error { | ||
return s.origin.ReleaseTickets(ctx, ticketIDs) | ||
} | ||
|
||
func (s *StoreWithTicketCache) AssignTickets(ctx context.Context, asgs []*pb.AssignmentGroup) error { | ||
return s.origin.AssignTickets(ctx, asgs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package statestore | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
cache "github.com/Code-Hex/go-generics-cache" | ||
"github.com/alicebob/miniredis/v2" | ||
"github.com/stretchr/testify/require" | ||
"open-match.dev/open-match/pkg/pb" | ||
) | ||
|
||
func TestTicketCache(t *testing.T) { | ||
mr := miniredis.RunT(t) | ||
ticketCache := cache.New[string, *pb.Ticket]() | ||
ttl := 500 * time.Millisecond | ||
redisStore := newTestRedisStore(t, mr.Addr()) | ||
store := NewStoreWithTicketCache(redisStore, ticketCache, WithTicketCacheTTL(ttl)) | ||
ctx := context.Background() | ||
|
||
require.NoError(t, store.CreateTicket(ctx, &pb.Ticket{Id: "t1"})) | ||
t1, err := store.GetTicket(ctx, "t1") | ||
require.NoError(t, err) | ||
require.Equal(t, "t1", t1.Id) | ||
|
||
require.NoError(t, redisStore.DeleteTicket(ctx, "t1")) | ||
|
||
// it can be retrieved from the cache even if deleted | ||
t1, err = store.GetTicket(ctx, "t1") | ||
require.NoError(t, err) | ||
require.Equal(t, "t1", t1.Id) | ||
|
||
time.Sleep(ttl + 10*time.Millisecond) | ||
|
||
t1, err = store.GetTicket(ctx, "t1") | ||
require.Error(t, err, ErrTicketNotFound) | ||
} |