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.

65 lines
1.6 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. Game Game
  10. Expiration int64
  11. }
  12. // Game object for binding with JSON POST body
  13. type Game struct {
  14. ID string `json:"id"`
  15. Player1 *Player `json:"player1"`
  16. Player2 *Player `json:"player2"`
  17. Turn *uuid.UUID `json:"turn,omitempty"`
  18. Draw *bool `json:"draw,omitempty"`
  19. Winner *uuid.UUID `json:"winner,omitempty"`
  20. Matrix Matrix `json:"matrix"`
  21. Tally []*Tally `json:"tally,omitempty"`
  22. NextGame string `json:"next_game"`
  23. PrevGame string `json:"previous_game"`
  24. }
  25. // Tally is a log of games won
  26. type Tally struct {
  27. Player Player `json:"player"`
  28. Matrix Matrix `json:"matrix"`
  29. }
  30. // Matrix is the game board and player uuids
  31. type Matrix [9]*uuid.UUID
  32. // Player object for binding with JSON POST body
  33. type Player struct {
  34. UUID uuid.UUID `json:"id"`
  35. Name string `json:"name,omitempty"`
  36. }
  37. // Cache is a simple in-memory cache for storing things
  38. type Cache struct {
  39. *cache
  40. }
  41. type cache struct {
  42. defaultExpiration time.Duration
  43. items map[string]Item
  44. mu sync.RWMutex
  45. onEvicted func(string, interface{})
  46. janitor *janitor
  47. }
  48. const (
  49. // NoExpiration For use with functions that take an expiration time.
  50. NoExpiration time.Duration = -1
  51. // DefaultExpiration For use with functions that take an expiration time.
  52. // Equivalent to passing in the same expiration duration as was given to
  53. // New() or NewFrom() when the cache was created (e.g. 5 minutes.)
  54. DefaultExpiration time.Duration = time.Hour * 1
  55. )