...

Source file src/github.com/gregjones/httpcache/redis/redis.go

Documentation: github.com/gregjones/httpcache/redis

     1  // Package redis provides a redis interface for http caching.
     2  package redis
     3  
     4  import (
     5  	"github.com/gomodule/redigo/redis"
     6  	"github.com/gregjones/httpcache"
     7  )
     8  
     9  // cache is an implementation of httpcache.Cache that caches responses in a
    10  // redis server.
    11  type cache struct {
    12  	redis.Conn
    13  }
    14  
    15  // cacheKey modifies an httpcache key for use in redis. Specifically, it
    16  // prefixes keys to avoid collision with other data stored in redis.
    17  func cacheKey(key string) string {
    18  	return "rediscache:" + key
    19  }
    20  
    21  // Get returns the response corresponding to key if present.
    22  func (c cache) Get(key string) (resp []byte, ok bool) {
    23  	item, err := redis.Bytes(c.Do("GET", cacheKey(key)))
    24  	if err != nil {
    25  		return nil, false
    26  	}
    27  	return item, true
    28  }
    29  
    30  // Set saves a response to the cache as key.
    31  func (c cache) Set(key string, resp []byte) {
    32  	c.Do("SET", cacheKey(key), resp)
    33  }
    34  
    35  // Delete removes the response with key from the cache.
    36  func (c cache) Delete(key string) {
    37  	c.Do("DEL", cacheKey(key))
    38  }
    39  
    40  // NewWithClient returns a new Cache with the given redis connection.
    41  func NewWithClient(client redis.Conn) httpcache.Cache {
    42  	return cache{client}
    43  }
    44  

View as plain text