github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/cache/provider.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package cache 5 6 import ( 7 "time" 8 ) 9 10 // CacheOptions contains options for initializaing a cache 11 type CacheOptions struct { 12 Size int 13 DefaultExpiry time.Duration 14 Name string 15 InvalidateClusterEvent string 16 Striped bool 17 StripedBuckets int 18 } 19 20 // Provider is a provider for Cache 21 type Provider interface { 22 // NewCache creates a new cache with given options. 23 NewCache(opts *CacheOptions) (Cache, error) 24 // Connect opens a new connection to the cache using specific provider parameters. 25 Connect() error 26 // Close releases any resources used by the cache provider. 27 Close() error 28 } 29 30 type cacheProvider struct { 31 } 32 33 // NewProvider creates a new CacheProvider 34 func NewProvider() Provider { 35 return &cacheProvider{} 36 } 37 38 // NewCache creates a new cache with given opts 39 func (c *cacheProvider) NewCache(opts *CacheOptions) (Cache, error) { 40 if opts.Striped { 41 return NewLRUStriped(LRUOptions{ 42 Name: opts.Name, 43 Size: opts.Size, 44 DefaultExpiry: opts.DefaultExpiry, 45 InvalidateClusterEvent: opts.InvalidateClusterEvent, 46 StripedBuckets: opts.StripedBuckets, 47 }) 48 } 49 return NewLRU(LRUOptions{ 50 Name: opts.Name, 51 Size: opts.Size, 52 DefaultExpiry: opts.DefaultExpiry, 53 InvalidateClusterEvent: opts.InvalidateClusterEvent, 54 }), nil 55 } 56 57 // Connect opens a new connection to the cache using specific provider parameters. 58 func (c *cacheProvider) Connect() error { 59 return nil 60 } 61 62 // Close releases any resources used by the cache provider. 63 func (c *cacheProvider) Close() error { 64 return nil 65 }