github.com/gogf/gf/v2@v2.7.4/os/gcache/gcache_cache.go (about)

     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  
     7  package gcache
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/gogf/gf/v2/util/gconv"
    13  )
    14  
    15  // Cache struct.
    16  type Cache struct {
    17  	localAdapter
    18  }
    19  
    20  // localAdapter is alias of Adapter, for embedded attribute purpose only.
    21  type localAdapter = Adapter
    22  
    23  // New creates and returns a new cache object using default memory adapter.
    24  // Note that the LRU feature is only available using memory adapter.
    25  func New(lruCap ...int) *Cache {
    26  	memAdapter := NewAdapterMemory(lruCap...)
    27  	c := &Cache{
    28  		localAdapter: memAdapter,
    29  	}
    30  	return c
    31  }
    32  
    33  // NewWithAdapter creates and returns a Cache object with given Adapter implements.
    34  func NewWithAdapter(adapter Adapter) *Cache {
    35  	return &Cache{
    36  		localAdapter: adapter,
    37  	}
    38  }
    39  
    40  // SetAdapter changes the adapter for this cache.
    41  // Be very note that, this setting function is not concurrent-safe, which means you should not call
    42  // this setting function concurrently in multiple goroutines.
    43  func (c *Cache) SetAdapter(adapter Adapter) {
    44  	c.localAdapter = adapter
    45  }
    46  
    47  // GetAdapter returns the adapter that is set in current Cache.
    48  func (c *Cache) GetAdapter() Adapter {
    49  	return c.localAdapter
    50  }
    51  
    52  // Removes deletes `keys` in the cache.
    53  func (c *Cache) Removes(ctx context.Context, keys []interface{}) error {
    54  	_, err := c.Remove(ctx, keys...)
    55  	return err
    56  }
    57  
    58  // KeyStrings returns all keys in the cache as string slice.
    59  func (c *Cache) KeyStrings(ctx context.Context) ([]string, error) {
    60  	keys, err := c.Keys(ctx)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return gconv.Strings(keys), nil
    65  }