Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
* Updated the documentation
* Reorganized the code
  • Loading branch information
faabiosr committed Apr 22, 2017
1 parent a5d3c26 commit 70f96b8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ package main

import (
"github.com/fabiorphp/cachego"
"github.com/bradfitz/gomemcache/memcache"
"github.com/bradfitz/gomemcache/memcache"
)

func main() {
Expand Down
30 changes: 22 additions & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
15 changes: 12 additions & 3 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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)

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
24 changes: 17 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down
24 changes: 17 additions & 7 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
15 changes: 12 additions & 3 deletions memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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{
Expand Down
26 changes: 18 additions & 8 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}

Expand All @@ -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)

Expand All @@ -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)

Expand Down
Loading

0 comments on commit 70f96b8

Please sign in to comment.