Ejemplo n.º 1
0
func TestGet(t *testing.T) {
	assert := assert.New(t)
	key := "key"

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)
	c.SetPrefix(testRedisPrefix)

	// set data
	b := helper.TestGobItem("valueTestGet")
	_, err := pool.Get().Do("SETEX", testRedisPrefix+key, 300, b)
	assert.Nil(err)

	// get data
	var result string
	ok := c.Get(key, &result)
	assert.True(ok)
	assert.Equal("valueTestGet", result)

	// nil value
	b = helper.TestGobItem(nil)
	_, err = pool.Get().Do("SETEX", testRedisPrefix+key, 300, b)
	assert.Nil(err)
	var result2 string
	ok = c.Get(key, &result2)
	assert.False(ok)
	assert.Empty(result2)
}
Ejemplo n.º 2
0
func TestSetExpire(t *testing.T) {
	assert := assert.New(t)
	key := "key"

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)
	c.SetPrefix(testRedisPrefix)

	err := c.SetExpire(key, "valueTestSetExpire", 1000)
	assert.Nil(err)

	// get data
	var v string
	var ok bool

	ok = c.Get(key, &v)
	assert.True(ok)

	time.Sleep(200 * time.Millisecond)
	ok = c.Get(key, &v)
	assert.True(ok)

	time.Sleep(1 * time.Second)
	ok = c.Get(key, &v)
	assert.False(ok)
}
Ejemplo n.º 3
0
func TestSet(t *testing.T) {
	assert := assert.New(t)
	key := "key"

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)
	c.SetPrefix(testRedisPrefix)

	err := c.Set(key, "valueTestSet")
	assert.NoError(err)

	// get data
	b, err := pool.Get().Do("GET", testRedisPrefix+key)
	assert.NoError(err)
	b, err = redis.Bytes(b, err)
	assert.NoError(err)

	buf := bytes.NewBuffer(b.([]byte))
	dec := gob.NewDecoder(buf)

	item := &eurekache.Item{}
	err = dec.Decode(&item)
	assert.NoError(err)
	assert.Equal("valueTestSet", item.Value)

	// delete data
	err = c.Set(key, nil)
	assert.NoError(err)

	b, err = pool.Get().Do("GET", testRedisPrefix+key)
	assert.NoError(err)
	assert.Nil(b)

}
Ejemplo n.º 4
0
func TestNewRedisCache(t *testing.T) {
	assert := assert.New(t)

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)

	assert.NotNil(c)
	assert.Equal(pool, c.pool)
	assert.Equal(c.dbno, "0")
	assert.EqualValues(c.defaultTTL, 0)
}
Ejemplo n.º 5
0
func TestConn(t *testing.T) {
	assert := assert.New(t)

	c := NewRedisCache(helper.TestGetPool())
	conn, err := c.conn()
	assert.NoError(err)
	assert.NotNil(conn)

	c.pool = nil
	conn, err = c.conn()
	assert.Equal(errNilPool, err)
	assert.Nil(conn)
}
Ejemplo n.º 6
0
func TestGetInterface(t *testing.T) {
	assert := assert.New(t)
	key := "key"

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)
	c.SetPrefix(testRedisPrefix)

	// set data
	b := helper.TestGobItem("valueTestGetInterface")
	_, err := pool.Get().Do("SETEX", testRedisPrefix+key, 300, b)
	assert.Nil(err)

	// get data
	v, ok := c.GetInterface(key)
	assert.True(ok)
	assert.Equal("valueTestGetInterface", v)
}
Ejemplo n.º 7
0
func TestIntegrationGet(t *testing.T) {
	assert := assert.New(t)
	key := "testintegrationget"
	val := "TestIntegrationGet"

	mc := memorycache.NewCacheTTL(3)
	mc.SetTTL(200)
	rc := rediscache.NewRedisCache(helper.TestGetPool())
	rc.SetTTL(1000)
	rc.SetPrefix(testRedisPrefix)

	e := eurekache.New()
	e.SetCacheSources([]eurekache.Cache{mc, rc})
	e.Set(key, val)

	var ok bool
	var result string

	// miss cache
	ok = e.Get("key", &result)
	assert.False(ok)
	assert.Empty(result)

	// hit memory
	ok = e.Get(key, &result)
	assert.True(ok)
	assert.Equal(val, result)

	// hit redis
	result = ""
	time.Sleep(300 * time.Millisecond)
	ok = e.Get(key, &result)
	assert.True(ok)
	assert.Equal(val, result)

	// cache expired
	result = ""
	time.Sleep(1000 * time.Millisecond)
	ok = e.Get(key, &result)
	assert.False(ok)
	assert.Equal("", result)
}
Ejemplo n.º 8
0
func TestGetGobBytes(t *testing.T) {
	assert := assert.New(t)
	key := "key"

	pool := helper.TestGetPool()
	c := NewRedisCache(pool)
	c.SetPrefix(testRedisPrefix)

	// set data
	b := helper.TestGobItem("valueTestGetGobBytes")
	_, err := pool.Get().Do("SETEX", testRedisPrefix+key, 300, b)
	assert.Nil(err)

	// get data
	b, ok := c.GetGobBytes(key)
	assert.True(ok)

	var result string
	buf := bytes.NewBuffer(b)
	dec := gob.NewDecoder(buf)
	err = dec.Decode(&result)
	assert.Nil(err)
	assert.Equal("valueTestGetGobBytes", result)
}