ITKeyword,专注技术干货聚合推荐

注册 | 登录

redis:string数据类型与操作

shamohua 2011-11-21

相关推荐:java中数据类型转换 Integer String Long Float Double Date

1如何将字串 String 转换成整数  int   A. 有两个方法: 1 ).  int  i  =  Integer.parseInt([String]); 或  i  =  Integer.parseInt([String],[ int  radix]);

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

DescriptionSET

key value

Set a key to a string valueGET

key

Return the string value of the keyGETSET

key value

Set a key to a string returning the old value of the keyMGET

key1 key2 ... keyN

Multi-get, return the strings values of the keysSETNX

key value

Set a key to a string value if the key does not existSETEX

key time value

Set+Expire combo commandMSET

key1 value1 key2 value2 ... keyN valueN

Set multiple keys to multiple values in a single atomic operationMSETNX

key1 value1 key2 value2 ... keyN valueN

Set multiple keys to multiple values in a single atomic operation if none of the keys already existINCR

key

Increment the integer value of keyINCRBY

key integer

Increment the integer value of key by integerDECR

key

Decrement the integer value of keyDECRBY

key integer

Decrement the integer value of key by integerAPPEND

key value

Append the specified string to the string stored at keySUBSTR

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"); {

//

相关推荐:3:redis的数据类型及操作

1: redis的是key-value 数据库,value的数据类型有: Lists (列表) Sets (集合) Sorted sets (有序集合) Hashes (哈希表)键值的数据类型决定了该键值支持的操作

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

相关推荐:[转]redis中各种数据类型对应的jedis操作命令

一、常用数据类型简介:       redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 String是最简单的类型,一个key对应一个value Stri

    redis的基本数据类型之一:string。类型说明定义:src/sds.h 39 struct sdshdr { 40 int len; 41 int free; 42 char buf[]; 43 };sds是Simple Dynamic Strings

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。