bucket.go 711 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package ccache
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. type bucket struct {
  7. sync.RWMutex
  8. lookup map[string]*Item
  9. }
  10. func (b *bucket) get(key string) *Item {
  11. b.RLock()
  12. defer b.RUnlock()
  13. return b.lookup[key]
  14. }
  15. func (b *bucket) set(key string, value interface{}, duration time.Duration) (*Item, *Item) {
  16. expires := time.Now().Add(duration).UnixNano()
  17. item := newItem(key, value, expires)
  18. b.Lock()
  19. defer b.Unlock()
  20. existing := b.lookup[key]
  21. b.lookup[key] = item
  22. return item, existing
  23. }
  24. func (b *bucket) delete(key string) *Item {
  25. b.Lock()
  26. defer b.Unlock()
  27. item := b.lookup[key]
  28. delete(b.lookup, key)
  29. return item
  30. }
  31. func (b *bucket) clear() {
  32. b.Lock()
  33. defer b.Unlock()
  34. b.lookup = make(map[string]*Item)
  35. }