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.

51 lines
1.1 KiB

  1. package operation
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/go/glog"
  7. )
  8. type VidInfo struct {
  9. Locations []Location
  10. NextRefreshTime time.Time
  11. }
  12. type VidCache struct {
  13. cache []VidInfo
  14. }
  15. func (vc *VidCache) Get(vid string) ([]Location, error) {
  16. id, err := strconv.Atoi(vid)
  17. if err != nil {
  18. glog.V(1).Infof("Unknown volume id %s", vid)
  19. return nil, err
  20. }
  21. if 0 < id && id <= len(vc.cache) {
  22. if vc.cache[id-1].Locations == nil {
  23. return nil, errors.New("Not Set")
  24. }
  25. if vc.cache[id-1].NextRefreshTime.Before(time.Now()) {
  26. return nil, errors.New("Expired")
  27. }
  28. return vc.cache[id-1].Locations, nil
  29. }
  30. return nil, errors.New("Not Found")
  31. }
  32. func (vc *VidCache) Set(vid string, locations []Location, duration time.Duration) {
  33. id, err := strconv.Atoi(vid)
  34. if err != nil {
  35. glog.V(1).Infof("Unknown volume id %s", vid)
  36. return
  37. }
  38. if id > len(vc.cache) {
  39. for i := id - len(vc.cache); i > 0; i-- {
  40. vc.cache = append(vc.cache, VidInfo{})
  41. }
  42. }
  43. if id > 0 {
  44. vc.cache[id-1].Locations = locations
  45. vc.cache[id-1].NextRefreshTime = time.Now().Add(duration)
  46. }
  47. }