redis:string数据类型与操作

    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 

#include 

#define OUT(x) std::cout<<#x<<" = "< init_non_cluster_client();

void test_strings(redis::client & c); 

int main(int argv, char* argc[]) 
{
	boost::shared_ptr 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: "<


 
  

你可能感兴趣的:(redis学习)