gcache_cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gcache
  7. import (
  8. "context"
  9. "github.com/gogf/gf/container/gvar"
  10. "github.com/gogf/gf/os/gtimer"
  11. "github.com/gogf/gf/util/gconv"
  12. "time"
  13. )
  14. // Cache struct.
  15. type Cache struct {
  16. adapter Adapter // Adapter for cache features.
  17. ctx context.Context // Context for operations.
  18. }
  19. // New creates and returns a new cache object using default memory adapter.
  20. // Note that the LRU feature is only available using memory adapter.
  21. func New(lruCap ...int) *Cache {
  22. memAdapter := newAdapterMemory(lruCap...)
  23. c := &Cache{
  24. adapter: memAdapter,
  25. }
  26. // Here may be a "timer leak" if adapter is manually changed from memory adapter.
  27. // Do not worry about this, as adapter is less changed and it dose nothing if it's not used.
  28. gtimer.AddSingleton(time.Second, memAdapter.syncEventAndClearExpired)
  29. return c
  30. }
  31. // Clone returns a shallow copy of current object.
  32. func (c *Cache) Clone() *Cache {
  33. return &Cache{
  34. adapter: c.adapter,
  35. ctx: c.ctx,
  36. }
  37. }
  38. // Ctx is a chaining function, which shallowly clones current object and sets the context
  39. // for next operation.
  40. func (c *Cache) Ctx(ctx context.Context) *Cache {
  41. newCache := c.Clone()
  42. newCache.ctx = ctx
  43. return newCache
  44. }
  45. // SetAdapter changes the adapter for this cache.
  46. // Be very note that, this setting function is not concurrent-safe, which means you should not call
  47. // this setting function concurrently in multiple goroutines.
  48. func (c *Cache) SetAdapter(adapter Adapter) {
  49. c.adapter = adapter
  50. }
  51. // GetVar retrieves and returns the value of `key` as gvar.Var.
  52. func (c *Cache) GetVar(key interface{}) (*gvar.Var, error) {
  53. v, err := c.Get(key)
  54. return gvar.New(v), err
  55. }
  56. // Removes deletes `keys` in the cache.
  57. // Deprecated, use Remove instead.
  58. func (c *Cache) Removes(keys []interface{}) error {
  59. _, err := c.Remove(keys...)
  60. return err
  61. }
  62. // KeyStrings returns all keys in the cache as string slice.
  63. func (c *Cache) KeyStrings() ([]string, error) {
  64. keys, err := c.Keys()
  65. if err != nil {
  66. return nil, err
  67. }
  68. return gconv.Strings(keys), nil
  69. }
  70. // KeyStrings returns all keys in the cache as string slice.
  71. func (c *Cache) getCtx() context.Context {
  72. if c.ctx == nil {
  73. return context.Background()
  74. }
  75. return c.ctx
  76. }