memory.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package cache
  2. import (
  3. "sync"
  4. )
  5. var (
  6. memCache sync.Map
  7. )
  8. // Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
  9. func Load(key interface{}) (value interface{}, ok bool) {
  10. return memCache.Load(key)
  11. }
  12. // Delete deletes the value for a key.
  13. func Delete(key interface{}) {
  14. memCache.Delete(key)
  15. }
  16. // LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
  17. func LoadAndDelete(key interface{}) (value interface{}, loaded bool) {
  18. return memCache.LoadAndDelete(key)
  19. }
  20. // LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
  21. func LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
  22. return memCache.LoadOrStore(key, value)
  23. }
  24. // Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
  25. func Range(f func(key, value interface{}) bool) {
  26. memCache.Range(f)
  27. }
  28. // Store sets the value for a key.
  29. func Store(key, value interface{}) {
  30. memCache.Store(key, value)
  31. }