A simple TicTacToe app with Golang backend and WebSockets gluing it all together.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

  1. package cache
  2. import (
  3. "sync"
  4. "time"
  5. uuid "github.com/satori/go.uuid"
  6. )
  7. // Item for caching
  8. type Item struct {
  9. Series Series
  10. Expiration int64
  11. }
  12. // Series object for binding with JSON POST body
  13. type Series struct {
  14. ID string `json:"id"`
  15. Turn *uuid.UUID `json:"turn,omitempty"`
  16. Matrix Matrix `json:"matrix"`
  17. Tally []*Matrix `json:"tally,omitempty"`
  18. LogTally *Matrix `json:"log_match,omitempty"`
  19. }
  20. // Matrix is the game board and player uuids
  21. type Matrix [9]*uuid.UUID
  22. // Cache is a simple in-memory cache for storing things
  23. type Cache struct {
  24. *cache
  25. }
  26. type cache struct {
  27. defaultExpiration time.Duration
  28. items map[string]Item
  29. mu sync.RWMutex
  30. onEvicted func(string, interface{})
  31. janitor *janitor
  32. }
  33. const (
  34. // NoExpiration For use with functions that take an expiration time.
  35. NoExpiration time.Duration = -1
  36. // DefaultExpiration For use with functions that take an expiration time.
  37. // Equivalent to passing in the same expiration duration as was given to
  38. // New() or NewFrom() when the cache was created (e.g. 5 minutes.)
  39. DefaultExpiration time.Duration = time.Hour * 1
  40. )