redis:list数据类型与操作

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) );
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值