redis数据类型之list:redis list数据类型是一个双向循环链表。redis.conf中的相关配置如下:
428 # Similarly to hashes, small lists are also encoded in a special way in order
429 # to save a lot of space. The special representation is only used when
430 # you are under the following limits:
431 list-max-ziplist-entries 512
432 list-max-ziplist-value 64
对于list的操作详见: http://redis.readthedocs.org/en/2.4/list.html
redis c++接口的调用:
#include "redisclient.h"
#include "tests/functions.h"
#include <iostream>
#include <boost/date_time.hpp>
#define OUT(x) std::cout<<#x<<" = "<<x<<std::endl;
boost::shared_ptr<redis::client> init_non_cluster_client();
void test_list(redis::client & c);
int main(int argv, char* argc[])
{
boost::shared_ptr<redis::client> shared_c;
shared_c = init_non_cluster_client();
redis::client& c = *shared_c;
test_list(c);
return 0;
}
void test_list(redis::client & c)
{
test("test redis list type.");
while(c.llen("list")>0) {
OUT(c.lpop("list"));
}
test("lpush & rpush & lpop & rpop & blpop & brpop");
{
OUT( c.lpush("list", "lpush1") );
OUT( c.lpush("list", "lpush2") );
OUT( c.rpush("list", "rpush1") );
OUT( c.rpush("list", "rpush2") );
OUT( c.llen("list") );
redis::client::string_vector out;
OUT( c.lrange("list", 0, 10, out) );
for(size_t i=0; i<out.size(); ++ i) {
OUT(out[i]);
}
OUT( c.lpop("list") );
OUT( c.rpop("list") );
OUT( c.llen("list") );
OUT( c.lpush("list", "lpush2") );
OUT( c.rpush("list", "rpush2") );
OUT( c.llen("list") );
OUT( c.blpop("list1", 1) );
OUT( c.blpop("list", 1) );
OUT( c.brpop("list1", 1) );
OUT( c.brpop("list", 1) );
OUT( c.llen("list") );
}
test("llen & lrange & ltrim");
{
OUT( c.llen("list") );
redis::client::string_vector out;
OUT( c.lrange("list", 0, 10, out) );
for(size_t i=0; i<out.size(); ++ i) {
OUT(out[i]);
}
c.ltrim("list", 0, 2);
OUT( c.lrange("list", 0, 10, out) );
for(size_t i=0; i<out.size(); ++ i) {
OUT(out[i]);
}
OUT( c.llen("list") );
}
test("lrem");
{
OUT( c.llen("list") );
OUT( c.lrem("list", 1, "lpush1") );
OUT( c.lrem("list", -1, "lpush2") );
OUT( c.llen("list") );
}
test("lset & lindex");
{
OUT( c.lindex("list", 0) );
c.lset("list", 0, "set new value of index 0");
OUT( c.lindex("list", 0) );
}
}