Skip to content
This repository was archived by the owner on Mar 21, 2019. It is now read-only.

Commit 3a1bf9a

Browse files
committed
init
0 parents  commit 3a1bf9a

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# cache memcache

Diff for: memcache.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2013 Beego Authors
2+
// Copyright 2014 The Macaron Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
// not use this file except in compliance with the License. You may obtain
6+
// a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
// License for the specific language governing permissions and limitations
14+
// under the License.
15+
16+
package cache
17+
18+
import (
19+
"strings"
20+
21+
"github.com/Unknwon/com"
22+
"github.com/bradfitz/gomemcache/memcache"
23+
24+
"github.com/tango-contrib/cache"
25+
)
26+
27+
// MemcacheCacher represents a memcache cache adapter implementation.
28+
type MemcacheCacher struct {
29+
c *memcache.Client
30+
}
31+
32+
func NewItem(key string, data []byte, expire int32) *memcache.Item {
33+
return &memcache.Item{
34+
Key: key,
35+
Value: data,
36+
Expiration: expire,
37+
}
38+
}
39+
40+
// Put puts value into cache with key and expire time.
41+
// If expired is 0, it lives forever.
42+
func (c *MemcacheCacher) Put(key string, val interface{}, expire int64) error {
43+
return c.c.Set(NewItem(key, []byte(com.ToStr(val)), int32(expire)))
44+
}
45+
46+
// Get gets cached value by given key.
47+
func (c *MemcacheCacher) Get(key string) interface{} {
48+
item, err := c.c.Get(key)
49+
if err != nil {
50+
return nil
51+
}
52+
return string(item.Value)
53+
}
54+
55+
// Delete deletes cached value by given key.
56+
func (c *MemcacheCacher) Delete(key string) error {
57+
return c.c.Delete(key)
58+
}
59+
60+
// Incr increases cached int-type value by given key as a counter.
61+
func (c *MemcacheCacher) Incr(key string) error {
62+
_, err := c.c.Increment(key, 1)
63+
return err
64+
}
65+
66+
// Decr decreases cached int-type value by given key as a counter.
67+
func (c *MemcacheCacher) Decr(key string) error {
68+
_, err := c.c.Decrement(key, 1)
69+
return err
70+
}
71+
72+
// IsExist returns true if cached value exists.
73+
func (c *MemcacheCacher) IsExist(key string) bool {
74+
_, err := c.c.Get(key)
75+
return err == nil
76+
}
77+
78+
// Flush deletes all cached data.
79+
func (c *MemcacheCacher) Flush() error {
80+
return c.c.FlushAll()
81+
}
82+
83+
// StartAndGC starts GC routine based on config string settings.
84+
// AdapterConfig: 127.0.0.1:9090;127.0.0.1:9091
85+
func (c *MemcacheCacher) StartAndGC(opt cache.Options) error {
86+
c.c = memcache.New(strings.Split(opt.AdapterConfig, ";")...)
87+
return nil
88+
}
89+
90+
func init() {
91+
cache.Register("memcache", &MemcacheCacher{})
92+
}

Diff for: memcache.goconvey

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignore

Diff for: memcache_test.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2014 The Macaron Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package cache
16+
17+
import (
18+
"net/http"
19+
"net/http/httptest"
20+
"testing"
21+
"time"
22+
23+
"github.com/Unknwon/com"
24+
"github.com/lunny/tango"
25+
. "github.com/smartystreets/goconvey/convey"
26+
27+
"github.com/tango-contrib/cache"
28+
)
29+
30+
type CacheAction struct {
31+
cache.Cache
32+
}
33+
34+
func (c *CacheAction) Get() {
35+
So(c.Put("uname", "unknwon", 1), ShouldBeNil)
36+
So(c.Put("uname2", "unknwon2", 1), ShouldBeNil)
37+
So(c.IsExist("uname"), ShouldBeTrue)
38+
39+
So(c.Cache.Get("404"), ShouldBeNil)
40+
So(c.Cache.Get("uname").(string), ShouldEqual, "unknwon")
41+
42+
time.Sleep(1 * time.Second)
43+
So(c.Cache.Get("uname"), ShouldBeNil)
44+
time.Sleep(1 * time.Second)
45+
So(c.Cache.Get("uname2"), ShouldBeNil)
46+
47+
So(c.Put("uname", "unknwon", 0), ShouldBeNil)
48+
So(c.Delete("uname"), ShouldBeNil)
49+
So(c.Cache.Get("uname"), ShouldBeNil)
50+
51+
So(c.Put("uname", "unknwon", 0), ShouldBeNil)
52+
So(c.Flush(), ShouldBeNil)
53+
So(c.Cache.Get("uname"), ShouldBeNil)
54+
}
55+
56+
type Cache2Action struct {
57+
cache.Cache
58+
}
59+
60+
func (c *Cache2Action) Get() {
61+
So(c.Incr("404"), ShouldNotBeNil)
62+
So(c.Decr("404"), ShouldNotBeNil)
63+
64+
So(c.Put("int", 0, 0), ShouldBeNil)
65+
So(c.Put("int64", int64(0), 0), ShouldBeNil)
66+
So(c.Put("string", "hi", 0), ShouldBeNil)
67+
68+
So(c.Incr("int"), ShouldBeNil)
69+
So(c.Incr("int64"), ShouldBeNil)
70+
71+
So(c.Decr("int"), ShouldBeNil)
72+
So(c.Decr("int64"), ShouldBeNil)
73+
74+
So(c.Incr("string"), ShouldNotBeNil)
75+
So(c.Decr("string"), ShouldNotBeNil)
76+
77+
So(com.StrTo(c.Cache.Get("int").(string)).MustInt(), ShouldEqual, 0)
78+
So(com.StrTo(c.Cache.Get("int64").(string)).MustInt64(), ShouldEqual, 0)
79+
80+
So(c.Flush(), ShouldBeNil)
81+
}
82+
83+
func Test_MemcacheCacher(t *testing.T) {
84+
Convey("Test memcache cache adapter", t, func() {
85+
opt := cache.Options{
86+
Adapter: "memcache",
87+
AdapterConfig: "127.0.0.1:9090",
88+
}
89+
90+
Convey("Basic operations", func() {
91+
t := tango.New()
92+
t.Use(cache.New(opt))
93+
94+
t.Get("/", new(CacheAction))
95+
96+
resp := httptest.NewRecorder()
97+
req, err := http.NewRequest("GET", "/", nil)
98+
So(err, ShouldBeNil)
99+
t.ServeHTTP(resp, req)
100+
})
101+
102+
Convey("Increase and decrease operations", func() {
103+
t := tango.New()
104+
t.Use(cache.New(opt))
105+
t.Get("/", new(Cache2Action))
106+
107+
resp := httptest.NewRecorder()
108+
req, err := http.NewRequest("GET", "/", nil)
109+
So(err, ShouldBeNil)
110+
t.ServeHTTP(resp, req)
111+
})
112+
})
113+
}

0 commit comments

Comments
 (0)