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.

59 lines
1.2 KiB

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