moving caching w/ TTL to its own package

This commit is contained in:
Brad Rydzewski
2015-10-20 16:45:24 -07:00
parent efcab1210b
commit eb04d418d9
14 changed files with 275 additions and 112 deletions

39
cache/cache.go vendored Normal file
View File

@@ -0,0 +1,39 @@
package cache
import (
"time"
"github.com/koding/cache"
"golang.org/x/net/context"
)
type Cache interface {
Get(string) (interface{}, error)
Set(string, interface{}) error
}
func Get(c context.Context, key string) (interface{}, error) {
return FromContext(c).Get(key)
}
func Set(c context.Context, key string, value interface{}) error {
return FromContext(c).Set(key, value)
}
// Default creates an in-memory cache with the default
// 24 hour expiration period.
func Default() Cache {
return cache.NewMemoryWithTTL(time.Hour * 24)
}
// NewTTL returns an in-memory cache with the specified
// ttl expiration period.
func NewTTL(t time.Duration) Cache {
return cache.NewMemoryWithTTL(t)
}
// NewTTL returns an in-memory cache with the specified
// ttl expiration period.
func NewLRU(size int) Cache {
return cache.NewLRU(size)
}

35
cache/cache_test.go vendored Normal file
View File

@@ -0,0 +1,35 @@
package cache
import (
"testing"
// "github.com/drone/drone/model"
"github.com/franela/goblin"
"github.com/gin-gonic/gin"
)
func TestCache(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Cache", func() {
var c *gin.Context
g.BeforeEach(func() {
c = new(gin.Context)
ToContext(c, Default())
})
g.It("Should set and get an item", func() {
Set(c, "foo", "bar")
v, e := Get(c, "foo")
g.Assert(v).Equal("bar")
g.Assert(e == nil).IsTrue()
})
g.It("Should return nil when item not found", func() {
v, e := Get(c, "foo")
g.Assert(v == nil).IsTrue()
g.Assert(e == nil).IsFalse()
})
})
}

23
cache/context.go vendored Normal file
View File

@@ -0,0 +1,23 @@
package cache
import (
"golang.org/x/net/context"
)
const key = "cache"
// Setter defines a context that enables setting values.
type Setter interface {
Set(string, interface{})
}
// FromContext returns the Cache associated with this context.
func FromContext(c context.Context) Cache {
return c.Value(key).(Cache)
}
// ToContext adds the Cache to this context if it supports
// the Setter interface.
func ToContext(c Setter, cache Cache) {
c.Set(key, cache)
}

75
cache/helper.go vendored Normal file
View File

@@ -0,0 +1,75 @@
package cache
import (
"fmt"
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
// GetRepos returns the user permissions to the named repository
// from the cache associated with the current context.
func GetPerms(c context.Context, user *model.User, owner, name string) *model.Perm {
key := fmt.Sprintf("perms:%s:%s/%s",
user.Login,
owner,
name,
)
val, err := FromContext(c).Get(key)
if err != nil {
return nil
}
return val.(*model.Perm)
}
// SetRepos adds the listof user permissions to the named repsotiory
// to the cache assocaited with the current context.
func SetPerms(c context.Context, user *model.User, perm *model.Perm, owner, name string) {
key := fmt.Sprintf("perms:%s:%s/%s",
user.Login,
owner,
name,
)
FromContext(c).Set(key, perm)
}
// GetRepos returns the list of user repositories from the cache
// associated with the current context.
func GetRepos(c context.Context, user *model.User) []*model.RepoLite {
key := fmt.Sprintf("repos:%s",
user.Login,
)
val, err := FromContext(c).Get(key)
if err != nil {
return nil
}
return val.([]*model.RepoLite)
}
// SetRepos adds the listof user repositories to the cache assocaited
// with the current context.
func SetRepos(c context.Context, user *model.User, repos []*model.RepoLite) {
key := fmt.Sprintf("repos:%s",
user.Login,
)
FromContext(c).Set(key, repos)
}
// GetSetRepos is a helper function that attempts to get the
// repository list from the cache first. If no data is in the
// cache or it is expired, it will remotely fetch the list of
// repositories and populate the cache.
// func GetSetRepos(c context.Context, user *model.User) ([]*model.RepoLite, error) {
// cache := FromContext(c).Repos()
// repos := FromContext(c).Repos().Get(user)
// if repos != nil {
// return repos, nil
// }
// var err error
// repos, err = remote.FromContext(c).Repos(user)
// if err != nil {
// return nil, err
// }
// cache.Set(user, repos)
// return repos, nil
// }

56
cache/helper_test.go vendored Normal file
View File

@@ -0,0 +1,56 @@
package cache
import (
"testing"
"github.com/drone/drone/model"
"github.com/franela/goblin"
"github.com/gin-gonic/gin"
)
func TestHelper(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Cache helpers", func() {
var c *gin.Context
g.BeforeEach(func() {
c = new(gin.Context)
ToContext(c, Default())
})
g.It("Should set and get permissions", func() {
SetPerms(c, fakeUser, fakePerm, "octocat", "Spoon-Knife")
v := GetPerms(c, fakeUser, "octocat", "Spoon-Knife")
g.Assert(v).Equal(fakePerm)
})
g.It("Should return nil if permissions if not found", func() {
v := GetPerms(c, fakeUser, "octocat", "Spoon-Knife")
g.Assert(v == nil).IsTrue()
})
g.It("Should set and get repositories", func() {
SetRepos(c, fakeUser, fakeRepos)
v := GetRepos(c, fakeUser)
g.Assert(v).Equal(fakeRepos)
})
g.It("Should return nil if repositories not found", func() {
v := GetRepos(c, fakeUser)
g.Assert(v == nil).IsTrue()
})
})
}
var (
fakeUser = &model.User{Login: "octocat"}
fakePerm = &model.Perm{true, true, true}
fakeRepos = []*model.RepoLite{
{Owner: "octocat", Name: "Hello-World"},
{Owner: "octocat", Name: "hello-world"},
{Owner: "octocat", Name: "Spoon-Knife"},
}
)