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.

41 lines
895 B

  1. package operation
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. )
  7. type VidInfo struct {
  8. Locations []Location
  9. NextRefreshTime time.Time
  10. }
  11. type VidCache struct {
  12. cache []VidInfo
  13. }
  14. func (vc *VidCache) Get(vid string) ([]Location, error) {
  15. id, _ := strconv.Atoi(vid)
  16. if 0 < id && id <= len(vc.cache) {
  17. if vc.cache[id-1].Locations == nil {
  18. return nil, errors.New("Not Set")
  19. }
  20. if vc.cache[id-1].NextRefreshTime.Before(time.Now()) {
  21. return nil, errors.New("Expired")
  22. }
  23. return vc.cache[id-1].Locations, nil
  24. }
  25. return nil, errors.New("Not Found")
  26. }
  27. func (vc *VidCache) Set(vid string, locations []Location, duration time.Duration) {
  28. id, _ := strconv.Atoi(vid)
  29. if id >= len(vc.cache) {
  30. for i := id - len(vc.cache); i > 0; i-- {
  31. vc.cache = append(vc.cache, VidInfo{})
  32. }
  33. }
  34. vc.cache[id-1].Locations = locations
  35. vc.cache[id-1].NextRefreshTime = time.Now().Add(duration)
  36. }