cache interface (#1805)
This commit is contained in:
		
							
								
								
									
										17
									
								
								cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| // Package cache is a caching interface | ||||
| package cache | ||||
|  | ||||
| type Cache interface { | ||||
| 	// Initialise options | ||||
| 	Init(...Option) error | ||||
| 	// Get a value | ||||
| 	Get(key string) (interface{}, error) | ||||
| 	// Set a value | ||||
| 	Set(key string, val interface{}) error | ||||
| 	// Delete a value | ||||
| 	Delete(key string) error | ||||
| 	// Name of the implementation | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| type Option struct{} | ||||
							
								
								
									
										56
									
								
								cache/memory/memory.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								cache/memory/memory.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| // Package memory is an in memory cache | ||||
| package memory | ||||
|  | ||||
| import ( | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/micro/go-micro/v2/cache" | ||||
| 	"github.com/micro/go-micro/v2/errors" | ||||
| ) | ||||
|  | ||||
| type memoryCache struct { | ||||
| 	// TODO: use a decent caching library | ||||
| 	sync.RWMutex | ||||
| 	values map[string]interface{} | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Init(opts ...cache.Option) error { | ||||
| 	// TODO: implement | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Get(key string) (interface{}, error) { | ||||
| 	m.RLock() | ||||
| 	defer m.RUnlock() | ||||
|  | ||||
| 	v, ok := m.values[key] | ||||
| 	if !ok { | ||||
| 		return nil, errors.NotFound("go.micro.cache", key+" not found") | ||||
| 	} | ||||
|  | ||||
| 	return v, nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Set(key string, val interface{}) error { | ||||
| 	m.Lock() | ||||
| 	m.values[key] = val | ||||
| 	m.Unlock() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Delete(key string) error { | ||||
| 	m.Lock() | ||||
| 	delete(m.values, key) | ||||
| 	m.Unlock() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) String() string { | ||||
| 	return "memory" | ||||
| } | ||||
|  | ||||
| func NewCache(opts ...cache.Option) cache.Cache { | ||||
| 	return &memoryCache{ | ||||
| 		values: make(map[string]interface{}), | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user