您的位置:首页 > 数据库 > Redis

redis:string数据类型与操作

2011-11-21 17:22 615 查看
redis的基本数据类型之一:string。

类型说明

定义:src/sds.h

39 struct sdshdr {
40     int len;
41     int free;
42     char buf[];
43 };


sds是Simple Dynamic Strings的缩写,即简单的动态字符串。其中,

len:buf数组的长度。

free:数组中剩余可用字节数,由此可以理解为什么string类型是二进制安全的了,因为它本质上就是个byte数组,当然可以包含任何数据了

buf:个char数组用于存贮实际的字符串内容,其实char和c#中的byte是等价的,都是一个字节。

基本操作

Command         Parameters             Description
SET                  key value               Set a key to a string value
GET                 key                        Return the string value of the key
GETSET           key value                Set a key to a string returning the old value of the key
MGET              key1 key2 ... keyN    Multi-get, return the strings values of the keys
SETNX            key value                Set a key to a string value if the key does not exist
SETEX            key time value         Set+Expire combo command
MSET              key1 value1 key2 value2 ... keyN valueN     Set multiple keys to multiple values in a single atomic operation
MSETNX          key1 value1 key2 value2 ... keyN valueN     Set multiple keys to multiple values in a single atomic operation if none of the keys already exist
INCR               key                          Increment the integer value of key
INCRBY           key integer             Increment the integer value of key by integer
DECR              key                       Decrement the integer value of key
DECRBY          key integer             Decrement the integer value of key by integer
APPEND           key value               Append the specified string to the string stored at key
SUBSTR          key start end         Return a substring of a larger string


详细说明请参考:

中文: http://redis.readthedocs.org/en/2.4/string.html#setbit
英文: http://redis.io/commands#string
下面是利用mrpi-redis-cplusplus-client,对上述基本操作加以验证:

#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_strings(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_strings(c);

return 0;
}

void test_strings(redis::client & c)
{
std::cout<<"test save keys: "<<std::endl;
std::string key = "key";
std::string value = "value string for test ";
std::string response;
test("set & get");
{
c.set(key, value);
response = c.get(key);
OUT(response);
}

test("append");
{
// append the key
value += "1 ";
c.append(key, value);
response = c.get(key);
OUT(response);
}

test("substr");
{
// get the substring
// params : key start index
// end index: -1 the end
// 获得区间start,end之间的字串
string second_half = c.substr(key, value.size(), -1);
OUT(second_half);
second_half = c.substr(key, value.size(), 2*value.size());
OUT(second_half);
}

test("exists");
{
std::string key2 = "key2";
std::string value2 = "value2";
c.set(key2, value2);
std::cout<<"check the key whether exist."<<std::endl;
bool bExist = c.exists(key2);
OUT(bExist);
OUT( c.exists("key_exist") );
if(!bExist) {
c.set("key2", "value2");
}
}

test("keys");
{
// 获得关键字以key开头的集合
redis::client::string_vector keys;
OUT(c.keys("key*", keys));
}
// infos
redis::server_info info;
//	std::cout<<"the server info:"<<std::endl;
//	c.info(info);

test("getset");
{
// get the key value, and set the new value
response = c.getset(key, "key_value_new");
OUT(response);
response = c.get(key);
OUT(response);
}

test("mset & mget");
{
redis::client::string_vector vkeys;
redis::client::string_vector vvalues;
vkeys.push_back("k1");
vkeys.push_back("k2");
vvalues.push_back("v1");
vvalues.push_back("v2");
c.mset(vkeys, vvalues);
redis::client::string_vector vvls;
c.mget(vkeys, vvls);
OUT(vvls.size());
}

test("setnx");
{
OUT(c.setnx(key, value));
OUT(c.setnx("key_setnx", "value_setnx"));
}

test("setex");
{
// 将值value关联到key,并将key的生存时间设为seconds(以秒为单位)
int sec = 5;
c.setex("key_setex", "value_setex", sec);
OUT(c.get("key_setex"));
sleep(2*sec);
OUT(c.get("key_setex"));
}

test("incr & inrcby");
{
// inrc,key对应的value。步长为1的自加运算
c.set("key_age", "20");
OUT(c.get("key_age"));
int step = 5;
OUT(c.incr("key_age"));
OUT(c.get("key_age"));
// inrc,key对应的value。步长为step的自加运算
OUT(c.incrby("key_age", step));
OUT(c.get("key_age"));
}

test("derc & dercby");
{
int step = 3;
OUT(c.get("key_age"));
OUT(c.decr("key_age"));
OUT(c.get("key_age"));
OUT(c.decrby("key_age", step));
OUT(c.get("key_age"));
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: