Skip to content

Commit

Permalink
Merge pull request #2 from joshuaslate/fix/mongo-field-export
Browse files Browse the repository at this point in the history
Solving implicit assignment of unexported field issue in cachego.Mongo
  • Loading branch information
faabiosr authored Jan 31, 2018
2 parents 70f96b8 + d699d79 commit d1a5f54
Show file tree
Hide file tree
Showing 15 changed files with 765 additions and 762 deletions.
79 changes: 39 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
Simple interface around cache drivers

## Installation

Cachego requires Go 1.5 or later.

```
go get github.com/fabiorphp/cachego
```

If you want to get an specific version, please use the example bellow:
If you want to get an specific version, please use the example below:

```
go get gopkg.in/fabiorphp/cachego.v0
```
Expand All @@ -26,15 +29,13 @@ package main

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

var cache cachego.Cache

func init() {
cache = &cachego.Memcached{
memcached.New("localhost:11211")
}
cache = cachego.NewMemcached(memcached.New("localhost:11211"))
}
```

Expand All @@ -45,17 +46,17 @@ package main

import (
"github.com/fabiorphp/cachego"
"gopkg.in/redis.v4"
"gopkg.in/redis.v4"
)

var cache cachego.Cache

func init() {
cache = &cachego.Redis{
redis.NewClient(&redis.Options{
cache = cachego.NewRedis(
redis.NewClient(&redis.Options{
Addr: ":6379",
}),
}
}),
)
}
```

Expand All @@ -71,9 +72,9 @@ import (
var cache cachego.Cache

func init() {
cache = &cachego.File{
cache = cachego.NewFile(
"/cache-dir/",
}
)
}
```

Expand All @@ -89,7 +90,7 @@ import (
var cache cachego.Cache

func init() {
cache = NewMapCache()
cache = NewMap()
}
```

Expand All @@ -100,17 +101,17 @@ package main

import (
"github.com/fabiorphp/cachego"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2"
)

var cache cachego.Cache

func init() {
session, _ := mgo.Dial(address)
session, _ := mgo.Dial(address)

cache = &cachego.Mongo{
session.DB("cache").C("cache"),
}
cache = cachego.NewMongo(
session.DB("cache").C("cache"),
)
}
```

Expand All @@ -126,32 +127,31 @@ import (
var cache cachego.Cache

func init() {
memacached := &cachego.Memcached{
memcached.New("localhost:11211")
}
memcached := cachego.NewMemcached(
memcached.New("localhost:11211"),
)

redis := &cachego.Redis{
redis.NewClient(&redis.Options{
redis := cachego.NewRedis(
redis.NewClient(&redis.Options{
Addr: ":6379",
}),
}

file := &cachego.File{
"/cache-dir/",
}

cache = &cachego.Chain{
[]cachego.Cache{
cachego.NewMapCache(),
memcached,
redis,
file,
},
}
}),
)

file := cachego.NewFile(
"/cache-dir/"
)

cache = cachego.NewChain(
cachego.NewMap(),
memcached,
redis,
file,
)
}
```

### Usage

```go
package main

Expand All @@ -176,11 +176,10 @@ func main() {
}
```


## Documentation

Read the full documentation at [https://godoc.org/github.com/fabiorphp/cachego](https://godoc.org/github.com/fabiorphp/cachego).

## License

This project is released under the MIT licence. See [LICENCE](https://github.com/fabiorphp/cachego/blob/master/LICENSE) for more details.
This project is released under the MIT licence. See [LICENSE](https://github.com/fabiorphp/cachego/blob/master/LICENSE) for more details.
33 changes: 17 additions & 16 deletions cache.go
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
}
)
99 changes: 52 additions & 47 deletions chain.go
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
}
Loading

0 comments on commit d1a5f54

Please sign in to comment.