-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from joshuaslate/fix/mongo-field-export
Solving implicit assignment of unexported field issue in cachego.Mongo
- Loading branch information
Showing
15 changed files
with
765 additions
and
762 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
package cachego | ||
|
||
import ( | ||
"time" | ||
"time" | ||
) | ||
|
||
type ( | ||
// Cache is the top-level cache interface | ||
Cache interface { | ||
// Contains check if a cached key exists | ||
Contains(key string) bool | ||
// Cache is the top-level cache interface | ||
Cache interface { | ||
|
||
// Delete remove the cached key | ||
Delete(key string) error | ||
// Contains check if a cached key exists | ||
Contains(key string) bool | ||
|
||
// Fetch retrieve the cached key value | ||
Fetch(key string) (string, error) | ||
// Delete remove the cached key | ||
Delete(key string) error | ||
|
||
// FetchMulti retrieve multiple cached keys value | ||
FetchMulti(keys []string) map[string]string | ||
// Fetch retrieve the cached key value | ||
Fetch(key string) (string, error) | ||
|
||
// Flush remove all cached keys | ||
Flush() error | ||
// FetchMulti retrieve multiple cached keys value | ||
FetchMulti(keys []string) map[string]string | ||
|
||
// Save cache a value by key | ||
Save(key string, value string, lifeTime time.Duration) error | ||
} | ||
// Flush remove all cached keys | ||
Flush() error | ||
|
||
// Save cache a value by key | ||
Save(key string, value string, lifeTime time.Duration) error | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,93 @@ | ||
package cachego | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ( | ||
// Chain storage for dealing with multiple cache storage in the same time | ||
Chain struct { | ||
drivers []Cache | ||
} | ||
// Chain storage for dealing with multiple cache storage in the same time | ||
Chain struct { | ||
drivers []Cache | ||
} | ||
) | ||
|
||
// NewChain - Create an instance of Chain | ||
func NewChain(drivers ...Cache) *Chain { | ||
return &Chain{drivers} | ||
} | ||
|
||
// 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) { | ||
return true | ||
} | ||
} | ||
for _, driver := range c.drivers { | ||
if driver.Contains(key) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
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 { | ||
return err | ||
} | ||
} | ||
for _, driver := range c.drivers { | ||
if err := driver.Delete(key); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
return nil | ||
} | ||
|
||
// Retrieves the value of the first cache storage found | ||
func (c *Chain) Fetch(key string) (string, error) { | ||
|
||
errs := []string{} | ||
errs := []string{} | ||
|
||
for _, driver := range c.drivers { | ||
if value, err := driver.Fetch(key); err == nil { | ||
return value, nil | ||
} else { | ||
errs = append(errs, err.Error()) | ||
} | ||
} | ||
for _, driver := range c.drivers { | ||
if value, err := driver.Fetch(key); err == nil { | ||
return value, nil | ||
} else { | ||
errs = append(errs, err.Error()) | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("Key not found in cache chain. Errors: %s", strings.Join(errs, ",")) | ||
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) | ||
result := make(map[string]string) | ||
|
||
for _, key := range keys { | ||
if value, err := c.Fetch(key); err == nil { | ||
result[key] = value | ||
} | ||
} | ||
for _, key := range keys { | ||
if value, err := c.Fetch(key); err == nil { | ||
result[key] = value | ||
} | ||
} | ||
|
||
return result | ||
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 { | ||
return err | ||
} | ||
} | ||
for _, driver := range c.drivers { | ||
if err := driver.Flush(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
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 { | ||
if err := driver.Save(key, value, lifeTime); err != nil { | ||
return err | ||
} | ||
} | ||
for _, driver := range c.drivers { | ||
if err := driver.Save(key, value, lifeTime); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
return nil | ||
} |
Oops, something went wrong.