lookup_vid_cache.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package operation
  2. import (
  3. "errors"
  4. "strconv"
  5. "sync"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/util/log"
  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. log.Debugf("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. log.Debugf("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. }