Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pool support to redis cookie #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type CacheStore interface {
}

type responseCache struct {
status int
header http.Header
data []byte
Status int
Header http.Header
Data []byte
}

type cachedWriter struct {
Expand All @@ -65,7 +65,7 @@ func urlEscape(prefix string, u string) string {
}

func newCachedWriter(store CacheStore, expire time.Duration, writer gin.ResponseWriter, key string) *cachedWriter {
return &cachedWriter{writer, 0, false, store, expire, key}
return &cachedWriter{writer, http.StatusOK, false, store, expire, key}
}

func (w *cachedWriter) WriteHeader(code int) {
Expand Down Expand Up @@ -117,13 +117,13 @@ func SiteCache(store CacheStore, expire time.Duration) gin.HandlerFunc {
if err := store.Get(key, &cache); err != nil {
c.Next()
} else {
c.Writer.WriteHeader(cache.status)
for k, vals := range cache.header {
c.Writer.WriteHeader(cache.Status)
for k, vals := range cache.Header {
for _, v := range vals {
c.Writer.Header().Add(k, v)
}
}
c.Writer.Write(cache.data)
c.Writer.Write(cache.Data)
}
}
}
Expand All @@ -141,13 +141,13 @@ func CachePage(store CacheStore, expire time.Duration, handle gin.HandlerFunc) g
c.Writer = writer
handle(c)
} else {
c.Writer.WriteHeader(cache.status)
for k, vals := range cache.header {
c.Writer.WriteHeader(cache.Status)
for k, vals := range cache.Header {
for _, v := range vals {
c.Writer.Header().Add(k, v)
}
}
c.Writer.Write(cache.data)
c.Writer.Write(cache.Data)
}
}
}
4 changes: 4 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func NewRedisCache(host string, password string, defaultExpiration time.Duration
return &RedisStore{pool, defaultExpiration}
}

func NewRedisCacheWithPool(pool *redis.Pool, defaultExpiration time.Duration) *RedisStore {
return &RedisStore{pool, defaultExpiration}
}

func (c *RedisStore) Set(key string, value interface{}, expires time.Duration) error {
return c.invoke(c.pool.Get().Do, key, value, expires)
}
Expand Down
11 changes: 10 additions & 1 deletion sessions/redis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package sessions

import (
"github.com/boj/redistore"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/sessions"
"gopkg.in/boj/redistore.v1"
)

type RedisStore interface {
Expand Down Expand Up @@ -30,6 +31,14 @@ func NewRedisStore(size int, network, address, password string, keyPairs ...[]by
return &redisStore{store}, nil
}

func NewRedisStoreWithPool(pool *redis.Pool, keyPairs ...[]byte) (RedisStore, error) {
store, err := redistore.NewRediStoreWithPool(pool, keyPairs...)
if err != nil {
return nil, err
}
return &redisStore{store}, nil
}

type redisStore struct {
*redistore.RediStore
}
Expand Down