Skip to content

Commit

Permalink
IN-DEV
Browse files Browse the repository at this point in the history
  • Loading branch information
faabiosr committed Nov 5, 2016
1 parent ef654d2 commit 68ae8be
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
91 changes: 91 additions & 0 deletions mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cachego

import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"time"
)

func NewMongo(collection *mgo.Collection) *Mongo {
collection.EnsureIndex(mgo.Index{
Key: []string{"key"},
Unique: true,
})

return &Mongo{collection}
}

type Mongo struct {
collection *mgo.Collection
}

type MongoContent struct {
Duration int64
Key string
Value string
}

func (m *Mongo) Contains(key string) bool {
if _, err := m.Fetch(key); err != nil {
return false
}

return true
}

func (m *Mongo) Delete(key string) error {
return m.collection.Remove(bson.M{"key": key})
}

func (m *Mongo) Fetch(key string) (string, error) {
content := &MongoContent{}

err := m.collection.Find(bson.M{"key": key}).One(content)

if err != nil {
return "", err
}

return content.Value, nil
}

func (m *Mongo) FetchMulti(keys []string) map[string]string {
result := make(map[string]string)

iter := m.collection.Find(bson.M{"key": bson.M{"$in": keys}}).Iter()

content := &MongoContent{}

for iter.Next(content) {
result[content.Key] = content.Value
}

return result
}

func (m *Mongo) Flush() error {
_, err := m.collection.RemoveAll(bson.M{})

return err
}

func (m *Mongo) Save(key string, value string, lifeTime time.Duration) error {
duration := int64(0)

if lifeTime > 0 {
duration = time.Now().Unix() + int64(lifeTime.Seconds())
}

content := &MongoContent{
duration,
key,
value,
}

if _, err := m.collection.Upsert(bson.M{"key": key}, content); err != nil {
return err
}

return nil

}
141 changes: 141 additions & 0 deletions mongo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package cachego

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2"
"net"
"testing"
)

type MongoTestSuite struct {
suite.Suite

assert *assert.Assertions
cache Cache
session *mgo.Session
}

func (s *MongoTestSuite) SetupTest() {
address := "localhost:27017"

if _, err := net.Dial("tcp", address); err != nil {
s.T().Skip()
}

session, err := mgo.Dial(address)

if err != nil {
s.T().Skip()
}

s.cache = NewMongo(session.DB("cache").C("cache"))
s.assert = assert.New(s.T())
}

//func (s *MongoTestSuite) TearDownTest() {
// s.session.Close()
//}

//func (s *MongoTestSuite) TestSaveThrowError() {
// memcached := memcache.New("127.0.0.1:22222")
//
// cache := &Mongo{memcached}
//
// s.assert.Error(cache.Save("foo", "bar", 0))
//}

func (s *MongoTestSuite) TestSave() {
s.assert.Nil(s.cache.Save("foo", "abar", 0))
}

//func (s *MongoTestSuite) TestFetchThrowError() {
// key := "foo"
// value := "bar"
//
// s.cache.Save(key, value, 0)
//
// memcached := memcache.New("127.0.0.1:22222")
// cache := &Mongo{memcached}
//
// result, err := cache.Fetch(key)
//
// s.assert.Error(err)
// s.assert.Empty(result)
//}
//
func (s *MongoTestSuite) TestFetch() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 0)

result, err := s.cache.Fetch(key)

s.assert.Nil(err)
s.assert.Equal(value, result)
}

func (s *MongoTestSuite) TestContains() {
s.cache.Save("foo", "bar", 0)

s.assert.True(s.cache.Contains("foo"))
s.assert.False(s.cache.Contains("bar"))
}

//func (s *MongoTestSuite) TestDeleteThrowError() {
// s.assert.Error(s.cache.Delete("bar"))
//}
//
func (s *MongoTestSuite) TestDelete() {
s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Delete("foo"))
s.assert.False(s.cache.Contains("foo"))
}

//func (s *MongoTestSuite) TestFlushThrowError() {
// memcached := memcache.New("127.0.0.1:22222")
//
// cache := &Mongo{memcached}
//
// s.assert.Error(cache.Flush())
//}

func (s *MongoTestSuite) TestFlush() {
s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Flush())
s.assert.False(s.cache.Contains("foo"))
}

//func (s *MongoTestSuite) TestFetchMultiReturnNoItemsWhenThrowError() {
// cache := &Mongo{
// memcache.New("127.0.0.1:22222"),
// }
//
// result := cache.FetchMulti([]string{"foo"})
//
// s.assert.Len(result, 0)
//}
//
func (s *MongoTestSuite) TestFetchMulti() {
s.cache.Save("foo", "bar", 0)
s.cache.Save("john", "doe", 0)

result := s.cache.FetchMulti([]string{"foo", "john"})

s.assert.Len(result, 2)
}

//func (s *MongoTestSuite) TestFetchMultiWhenOnlyOneOfKeysExists() {
// s.cache.Save("foo", "bar", 0)
//
// result := s.cache.FetchMulti([]string{"foo", "alice"})
//
// s.assert.Len(result, 1)
//}
//
func TestMongoRunSuite(t *testing.T) {
suite.Run(t, new(MongoTestSuite))
}

0 comments on commit 68ae8be

Please sign in to comment.