diff --git a/README.md b/README.md index 6e08668..6dabddd 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ package main import ( "github.com/fabiorphp/cachego" - "github.com/bradfitz/gomemcache/memcache" + "github.com/bradfitz/gomemcache/memcache" ) func main() { diff --git a/cache.go b/cache.go index 00ad91d..10f7a4b 100644 --- a/cache.go +++ b/cache.go @@ -4,11 +4,25 @@ import ( "time" ) -type Cache interface { - Contains(key string) bool - Delete(key string) error - Fetch(key string) (string, error) - FetchMulti(keys []string) map[string]string - Flush() error - Save(key string, value string, lifeTime time.Duration) error -} +type ( + // Cache is the top-level cache interface + Cache interface { + // Contains check if a cached key exists + Contains(key string) bool + + // Delete remove the cached key + Delete(key string) error + + // Fetch retrieve the cached key value + Fetch(key string) (string, error) + + // FetchMulti retrieve multiple cached keys value + FetchMulti(keys []string) map[string]string + + // Flush remove all cached keys + Flush() error + + // Save cache a value by key + Save(key string, value string, lifeTime time.Duration) error + } +) diff --git a/chain.go b/chain.go index 86951f8..ab9d1b1 100644 --- a/chain.go +++ b/chain.go @@ -6,10 +6,14 @@ import ( "time" ) -type Chain struct { - drivers []Cache -} +type ( + // Chain storage for dealing with multiple cache storage in the same time + Chain struct { + drivers []Cache + } +) +// Check if cached key exists in one of cache storage func (c *Chain) Contains(key string) bool { for _, driver := range c.drivers { if driver.Contains(key) { @@ -20,6 +24,7 @@ func (c *Chain) Contains(key string) bool { return false } +// Delete the cached key in all cache storages func (c *Chain) Delete(key string) error { for _, driver := range c.drivers { if err := driver.Delete(key); err != nil { @@ -30,6 +35,7 @@ func (c *Chain) Delete(key string) error { return nil } +// Retrieves the value of the first cache storage found func (c *Chain) Fetch(key string) (string, error) { errs := []string{} @@ -45,6 +51,7 @@ func (c *Chain) Fetch(key string) (string, error) { return "", fmt.Errorf("Key not found in cache chain. Errors: %s", strings.Join(errs, ",")) } +// Retrieve multiple cached value from keys of the first cache storage found func (c *Chain) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -57,6 +64,7 @@ func (c *Chain) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys of the all cache storages func (c *Chain) Flush() error { for _, driver := range c.drivers { if err := driver.Flush(); err != nil { @@ -67,6 +75,7 @@ func (c *Chain) Flush() error { return nil } +// Save a value in all cache storages by key func (c *Chain) Save(key string, value string, lifeTime time.Duration) error { for _, driver := range c.drivers { diff --git a/file.go b/file.go index 1e8d756..136edb3 100644 --- a/file.go +++ b/file.go @@ -11,14 +11,18 @@ import ( "time" ) -type File struct { - dir string -} +type ( + // File store for caching data + File struct { + dir string + } -type FileContent struct { - Duration int64 `json:"duration"` - Data string `json:"data, omitempty"` -} + // File content it's a structure of cached value + FileContent struct { + Duration int64 `json:"duration"` + Data string `json:"data, omitempty"` + } +) func (f *File) createName(key string) string { h := sha256.New() @@ -61,6 +65,7 @@ func (f *File) read(key string) (*FileContent, error) { return content, nil } +// Check if cached key exists in File storage func (f *File) Contains(key string) bool { if _, err := f.read(key); err != nil { @@ -70,6 +75,7 @@ func (f *File) Contains(key string) bool { return true } +// Delete the cached key from File storage func (f *File) Delete(key string) error { _, err := os.Stat( f.createName(key), @@ -90,6 +96,7 @@ func (f *File) Delete(key string) error { return nil } +// Retrieve the cached value from key of the File storage func (f *File) Fetch(key string) (string, error) { content, err := f.read(key) @@ -100,6 +107,7 @@ func (f *File) Fetch(key string) (string, error) { return content.Data, nil } +// Retrieve multiple cached value from keys of the File storage func (f *File) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -112,6 +120,7 @@ func (f *File) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys in File storage func (f *File) Flush() error { dir, err := os.Open(f.dir) @@ -130,6 +139,7 @@ func (f *File) Flush() error { return nil } +// Save a value in File storage by key func (f *File) Save(key string, value string, lifeTime time.Duration) error { duration := int64(0) diff --git a/map.go b/map.go index 78b44f6..d9a11cb 100644 --- a/map.go +++ b/map.go @@ -5,15 +5,19 @@ import ( "time" ) -type MapItem struct { - data string - duration int64 -} +type ( + MapItem struct { + data string + duration int64 + } -type Map struct { - storage map[string]*MapItem -} + // Map store the data in memory without external server + Map struct { + storage map[string]*MapItem + } +) +// Create an instance of Map storage func NewMapCache() *Map { storage := make(map[string]*MapItem) @@ -39,6 +43,7 @@ func (m *Map) read(key string) (*MapItem, error) { return item, nil } +// Check if cached key exists in Map storage func (m *Map) Contains(key string) bool { if _, err := m.Fetch(key); err != nil { return false @@ -47,11 +52,13 @@ func (m *Map) Contains(key string) bool { return true } +// Delete the cached key from Map storage func (m *Map) Delete(key string) error { delete(m.storage, key) return nil } +// Retrieve the cached value from key of the Map storage func (m *Map) Fetch(key string) (string, error) { item, err := m.read(key) @@ -62,6 +69,7 @@ func (m *Map) Fetch(key string) (string, error) { return item.data, nil } +// Retrieve multiple cached value from keys of the Map storage func (m *Map) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -74,11 +82,13 @@ func (m *Map) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys in Map storage func (m *Map) Flush() error { m.storage = make(map[string]*MapItem) return nil } +// Save a value in Map storage by key func (m *Map) Save(key string, value string, lifeTime time.Duration) error { duration := int64(0) diff --git a/memcached.go b/memcached.go index e2bb92a..e4fa385 100644 --- a/memcached.go +++ b/memcached.go @@ -5,10 +5,14 @@ import ( "time" ) -type Memcached struct { - driver *memcache.Client -} +type ( + // Memcached it's a wrap around the memcached driver + Memcached struct { + driver *memcache.Client + } +) +// Check if cached key exists in Memcached storage func (m *Memcached) Contains(key string) bool { if _, err := m.Fetch(key); err != nil { return false @@ -17,10 +21,12 @@ func (m *Memcached) Contains(key string) bool { return true } +// Delete the cached key from Memcached storage func (m *Memcached) Delete(key string) error { return m.driver.Delete(key) } +// Retrieve the cached value from key of the Memcached storage func (m *Memcached) Fetch(key string) (string, error) { item, err := m.driver.Get(key) @@ -33,6 +39,7 @@ func (m *Memcached) Fetch(key string) (string, error) { return value, nil } +// Retrieve multiple cached value from keys of the Memcached storage func (m *Memcached) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -49,10 +56,12 @@ func (m *Memcached) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys in Memcached storage func (m *Memcached) Flush() error { return m.driver.FlushAll() } +// Save a value in Memcached storage by key func (m *Memcached) Save(key string, value string, lifeTime time.Duration) error { err := m.driver.Set( &memcache.Item{ diff --git a/mongo.go b/mongo.go index e9c4c74..761bce8 100644 --- a/mongo.go +++ b/mongo.go @@ -7,16 +7,21 @@ import ( "time" ) -type Mongo struct { - collection *mgo.Collection -} +type ( + // Mongo it's a wrap around the mgo driver + Mongo struct { + collection *mgo.Collection + } -type MongoContent struct { - Duration int64 - Key string `bson:"_id"` - Value string -} + // MongoContent it's a bson structure of cached value + MongoContent struct { + Duration int64 + Key string `bson:"_id"` + Value string + } +) +// Check if cached key exists in Mongo storage func (m *Mongo) Contains(key string) bool { if _, err := m.Fetch(key); err != nil { return false @@ -25,10 +30,12 @@ func (m *Mongo) Contains(key string) bool { return true } +// Delete the cached key from Mongo storage func (m *Mongo) Delete(key string) error { return m.collection.Remove(bson.M{"_id": key}) } +// Retrieve the cached value from key of the Mongo storage func (m *Mongo) Fetch(key string) (string, error) { content := &MongoContent{} @@ -50,6 +57,7 @@ func (m *Mongo) Fetch(key string) (string, error) { return content.Value, nil } +// Retrieve multiple cached value from keys of the Mongo storage func (m *Mongo) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -64,12 +72,14 @@ func (m *Mongo) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys in Mongo storage func (m *Mongo) Flush() error { _, err := m.collection.RemoveAll(bson.M{}) return err } +// Save a value in Mongo storage by key func (m *Mongo) Save(key string, value string, lifeTime time.Duration) error { duration := int64(0) diff --git a/redis.go b/redis.go index 65d0f84..c8fd20c 100644 --- a/redis.go +++ b/redis.go @@ -5,10 +5,14 @@ import ( "time" ) -type Redis struct { - driver *redis.Client -} +type ( + // Redis it's a wrap around the redis driver + Redis struct { + driver *redis.Client + } +) +// Check if cached key exists in Redis storage func (r *Redis) Contains(key string) bool { status, err := r.driver.Exists(key).Result() @@ -19,10 +23,12 @@ func (r *Redis) Contains(key string) bool { return status } +// Delete the cached key from Redis storage func (r *Redis) Delete(key string) error { return r.driver.Del(key).Err() } +// Retrieve the cached value from key of the Redis storage func (r *Redis) Fetch(key string) (string, error) { value, err := r.driver.Get(key).Result() @@ -33,6 +39,7 @@ func (r *Redis) Fetch(key string) (string, error) { return value, nil } +// Retrieve multiple cached value from keys of the Redis storage func (r *Redis) FetchMulti(keys []string) map[string]string { result := make(map[string]string) @@ -51,10 +58,12 @@ func (r *Redis) FetchMulti(keys []string) map[string]string { return result } +// Remove all cached keys in Redis storage func (r *Redis) Flush() error { return r.driver.FlushAll().Err() } +// Save a value in Redis storage by key func (r *Redis) Save(key string, value string, lifeTime time.Duration) error { return r.driver.Set(key, value, lifeTime).Err() }