acl/lib_acl_cpp/include/acl_cpp/redis/redis_key.hpp

411 lines
15 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2015-01-14 23:35:23 +08:00
#include <vector>
2017-06-02 14:47:24 +08:00
#include "../stdlib/string.hpp"
#include "redis_command.hpp"
2014-11-19 00:25:21 +08:00
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
2014-11-19 00:25:21 +08:00
namespace acl {
class redis_client;
2015-04-12 21:37:17 +08:00
class redis_client_cluster;
2015-01-14 23:35:23 +08:00
// redis 服务支持的数据类型分类
2015-04-12 21:37:17 +08:00
// the data type supported by redis
2015-01-14 23:35:23 +08:00
typedef enum
{
2015-04-12 21:37:17 +08:00
REDIS_KEY_NONE, // none
2015-01-14 23:35:23 +08:00
REDIS_KEY_STRING, // string
REDIS_KEY_HASH, // hash
REDIS_KEY_LIST, // list
REDIS_KEY_SET, // set
REDIS_KEY_ZSET // sorted set
} redis_key_t;
class ACL_CPP_API redis_key : virtual public redis_command
2014-11-19 00:25:21 +08:00
{
public:
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command()
*/
redis_key(void);
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command(redis_client*)
*/
redis_key(redis_client* conn);
2015-03-29 19:27:41 +08:00
/**
2016-05-22 09:10:34 +08:00
* see redis_command::redis_command(redis_client_cluster*, size_t)
2015-03-29 19:27:41 +08:00
*/
redis_key(redis_client_cluster* cluster, size_t max_conns = 0);
2015-03-29 19:27:41 +08:00
virtual ~redis_key(void);
2015-01-14 23:35:23 +08:00
/**
* KEY NULL
2015-04-12 21:37:17 +08:00
* delete one or some keys from redis, for deleting a variable
* number of keys, the last key must be NULL indicating the end
* of the variable args
* @return {int} KEY
* 0: KEY
* -1:
* >0: KEY KEY
2015-04-12 21:37:17 +08:00
* return the number of keys been deleted, return value as below:
* 0: none key be deleted
* -1: error happened
* >0: the number of keys been deleted
*
2015-01-14 23:35:23 +08:00
*/
int del_one(const char* key);
int del_one(const char* key, size_t len);
int del(const char* key);
2015-01-14 23:35:23 +08:00
int del(const std::vector<string>& keys);
int del(const std::vector<const char*>& keys);
int del(const char* keys[], size_t argc);
int del(const char* keys[], const size_t lens[], size_t argc);
int del_keys(const char* first_key, ...);
int del_keys(const std::vector<string>& keys);
int del_keys(const std::vector<const char*>& keys);
int del_keys(const char* keys[], size_t argc);
int del_keys(const char* keys[], const size_t lens[], size_t argc);
2015-01-14 23:35:23 +08:00
/**
* key 使 RESTORE
* Redis
2015-04-12 21:37:17 +08:00
* serialize the object associate with the given key, and get the
* value after serializing, RESTORE command can be used to
* deserialize by the value
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param out {string&}
2015-04-12 21:37:17 +08:00
* buffur used to store the result
* @return {int}
2015-04-12 21:37:17 +08:00
* the length of the data after serializing
*/
int dump(const char* key, string& out);
/**
* KEY
2015-04-12 21:37:17 +08:00
* check if the key exists in redis
* @param key {const char*} KEY
2015-04-12 21:37:17 +08:00
* the key
* @return {bool} true
2015-04-12 21:37:17 +08:00
* true returned if key existing, false if error or not existing
*/
bool exists(const char* key);
2015-01-14 23:35:23 +08:00
/**
* KEY
2015-04-12 21:37:17 +08:00
* set a key's time to live in seconds
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param n {int}
2015-04-12 21:37:17 +08:00
* lief cycle in seconds
* @return {int}
2015-04-12 21:37:17 +08:00
* return value as below:
* > 0:
2015-04-12 21:37:17 +08:00
* set successfully
* 0 key
2015-04-12 21:37:17 +08:00
* the key doesn't exist
* < 0:
2015-04-12 21:37:17 +08:00
* error happened
2015-01-14 23:35:23 +08:00
*/
int expire(const char* key, int n);
/**
* UNIX KEY
2015-04-12 21:37:17 +08:00
* set the expiration for a key as a UNIX timestamp
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param stamp {time_t} UNIX 1970
2015-04-12 21:37:17 +08:00
* an absolute Unix timestamp (seconds since January 1, 1970).
* @return {int}
2015-04-12 21:37:17 +08:00
* return value:
* 1:
2015-04-12 21:37:17 +08:00
* the timeout was set
* 0: key
2015-04-12 21:37:17 +08:00
* the key doesn't exist or the timeout couldn't be set
* -1:
2015-04-12 21:37:17 +08:00
* error happened
*/
int expireat(const char* key, time_t stamp);
/**
* pattern key
2015-04-12 21:37:17 +08:00
* find all keys matching the given pattern
* @param pattern {const char*}
2015-04-12 21:37:17 +08:00
* the give matching pattern
* @param out {std::vector<string>*} NULL
2015-04-12 21:37:17 +08:00
* store the matched keys
* @return {int} 0--<0 --
2015-04-12 21:37:17 +08:00
* return the number of the matched keys, 0 if none, < 0 if error
*
* KEYS * key
* KEYS h?llo hello hallo hxllo
* KEYS h*llo hllo heeeeello
* KEYS h[ae]llo hello hallo hillo
2015-01-30 22:58:13 +08:00
*
*
* 1 get_value
* 2 get_child (redis_result
* redis_result::argv_to_string
* 3 get_result redis_result
* redis_result::get_child 2
*
* 4 get_children redis_result
* argv_to_string
* 5
*/
2015-01-30 22:58:13 +08:00
int keys_pattern(const char* pattern, std::vector<string>* out);
2015-01-23 17:23:04 +08:00
/**
* redis-server redis-server
2015-04-12 21:37:17 +08:00
* atomically transfer a key from a redis instance to another one
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param addr {const char*} redis-server ip:port
2015-04-12 21:37:17 +08:00
* the destination redis instance's address, format: ip:port
* @param dest_db {unsigned} redis-server ID
2015-04-12 21:37:17 +08:00
* the databases ID in destination redis
* @param timeout {unsigned} ()
2015-04-12 21:37:17 +08:00
* timeout(microseconds) in transfering
* @param option {const char*} COPY REPLACE
2015-04-12 21:37:17 +08:00
* transfer option: COPY or REPLACE
* @return {bool}
2015-04-12 21:37:17 +08:00
* if transfering successfully
2015-01-23 17:23:04 +08:00
*/
bool migrate(const char* key, const char* addr, unsigned dest_db,
unsigned timeout, const char* option = NULL);
/**
* redis-server
2015-04-12 21:37:17 +08:00
* move a key to another database
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param dest_db {unsigned} ID
2015-04-12 21:37:17 +08:00
* the destination database
* @return {int} -1: 0
* 1
2015-04-12 21:37:17 +08:00
* if moving succcessfully. -1 if error, 0 if moving failed because
* the same key already exists, 1 if successful
2015-01-23 17:23:04 +08:00
*/
int move(const char* key, unsigned dest_db);
/**
* key
2015-04-12 21:37:17 +08:00
* get the referring count of the object, which just for debugging
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @return {int} 0 key < 0
2015-04-12 21:37:17 +08:00
* 0 if key not exists, < 0 if error
*/
int object_refcount(const char* key);
/**
* key 使
2015-04-12 21:37:17 +08:00
* get the internal storing of the object assosicate with the key
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param out {string&}
2015-04-12 21:37:17 +08:00
* store the result
* @return {bool}
2015-04-12 21:37:17 +08:00
* if successful
*/
bool object_encoding(const char* key, string& out);
/**
* key (idle )
2015-04-12 21:37:17 +08:00
* get the key's idle time in seconds since its first stored
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @return {int} < 0
2015-04-12 21:37:17 +08:00
* 0 if error happened
*/
int object_idletime(const char* key);
/**
* key key "易失的"( key )
* "持久的"( key )
2015-04-12 21:37:17 +08:00
* remove the expiration from a key
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @return {int}
2015-04-12 21:37:17 +08:00
* the value returned as below:
* 1 --
2015-04-12 21:37:17 +08:00
* set ok
* 0 -- key
2015-04-12 21:37:17 +08:00
* the key not exists or not be set expiration
* -1 --
2015-04-12 21:37:17 +08:00
* error happened
*/
int persist(const char* key);
/**
* KEY
2015-04-12 21:37:17 +08:00
* set a key's time to live in milliseconds
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param n {int}
2015-04-12 21:37:17 +08:00
* time to live in milliseconds
* @return {int}
2015-04-12 21:37:17 +08:00
* value returned as below:
* > 0:
2015-04-12 21:37:17 +08:00
* set successfully
* 0 key
2015-04-12 21:37:17 +08:00
* the key doesn't exist
* < 0:
2015-04-12 21:37:17 +08:00
* error happened
*/
int pexpire(const char* key, int n);
/**
* key unix
2015-04-12 21:37:17 +08:00
* set the expiration for a key as UNIX timestamp specified
* in milliseconds
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @param n {long long int} UNIX 1970
2015-04-12 21:37:17 +08:00
* the UNIX timestamp in milliseconds from 1970.1.1
* @return {int}
2015-04-12 21:37:17 +08:00
* value resturned as below:
* > 0:
2015-04-12 21:37:17 +08:00
* set successfully
* 0 key
2015-04-12 21:37:17 +08:00
* the key doesn't exist
* < 0:
2015-04-12 21:37:17 +08:00
* error happened
*/
int pexpireat(const char* key, long long int n);
/**
* KEY
2015-04-12 21:37:17 +08:00
* get the time to live for a key in milliseconds
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @return {int}
2015-04-12 21:37:17 +08:00
* value returned as below:
* >0: key
2015-04-12 21:37:17 +08:00
* the time to live for a key in milliseconds
* -3
2015-04-12 21:37:17 +08:00
* error happened
* -2key
2015-04-12 21:37:17 +08:00
* the key doesn't exist
* -1 key
2015-04-12 21:37:17 +08:00
* th key were not be set expiration
* redis-server 2.8 key -1
2015-04-12 21:37:17 +08:00
* notice: for redis version before 2.8, -1 will be returned if the
* key doesn't exist or the key were not be set expiration.
*/
long long int pttl(const char* key);
/**
* () key
2015-04-12 21:37:17 +08:00
* return a random key from the keyspace
@param buf {string&} KEY
2015-04-12 21:37:17 +08:00
* store the key
* @return {bool} key false
2015-04-12 21:37:17 +08:00
* true on success, or false be returned
*/
bool randmkey(string& buf);
/**
* key newkey
2015-04-12 21:37:17 +08:00
* rename a key
* @return {bool}
2015-04-12 21:37:17 +08:00
* true on success, or error happened
*/
bool rename_key(const char* key, const char* newkey);
/**
* newkey key newkey
2015-04-12 21:37:17 +08:00
* rename a key only if the new key does not exist
* @param key {const char*} key
* @param newkey {const char*} key
* @return {int} > 0: 0 key < 0
* return value > 0 on success, < 0 on error, == 0 when newkey exists
*/
int renamenx(const char* key, const char* newkey);
/**
* key
2015-04-12 21:37:17 +08:00
* create a key using the provided serialized value, previously
* obtained by using DUMP
* @param ttl {int} key ttl 0
*
2015-04-12 21:37:17 +08:00
* the time to live for the key in milliseconds, if tll is 0,
* expiration will not be set
* @param replace {bool} key
2015-04-12 21:37:17 +08:00
* if the key already exists, this parameter decides if replacing
* the existing key
* @return {bool}
2015-04-12 21:37:17 +08:00
* true on success, false on error
*/
bool restore(const char* key, const char* value, size_t len,
int ttl, bool replace = false);
2015-01-14 23:35:23 +08:00
/**
* KEY
2015-04-12 21:37:17 +08:00
* get the time to live for a key in seconds
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the key
* @return {int}
2015-04-12 21:37:17 +08:00
* return value as below:
* > 0: key
2015-04-12 21:37:17 +08:00
* the time to live for a key in seconds
* -3
2015-04-12 21:37:17 +08:00
* error happened
* -2key
2015-04-12 21:37:17 +08:00
* the key doesn't exist
* -1 key
2015-04-12 21:37:17 +08:00
* the key were not be set expiration
* redis-server 2.8 key -1
2015-04-12 21:37:17 +08:00
* notice: for the redis version before 2.8, -1 will be returned
* if the key doesn't exist or the key were not be set expiration
2015-01-14 23:35:23 +08:00
*/
2015-01-23 17:23:04 +08:00
int ttl(const char* key);
2015-01-14 23:35:23 +08:00
/**
* KEY
2015-04-12 21:37:17 +08:00
* get the the type stored at key
* @para key {const char*} KEY
2015-04-12 21:37:17 +08:00
* the key
* @return {redis_key_t} KEY
2015-04-12 21:37:17 +08:00
* return redis_key_t defined above as REDIS_KEY_
2015-01-14 23:35:23 +08:00
*/
redis_key_t type(const char* key);
/**
*
2015-04-12 21:37:17 +08:00
* incrementally iterate the keys space in the specified database
* @param cursor {int} 0
2015-04-12 21:37:17 +08:00
* the iterating cursor beginning with 0
* @param out {std::vector<acl::string>&}
*
*
2015-04-12 21:37:17 +08:00
* string array storing the results, the array will be cleared
* internal and the string result will be appened to the array
* @param pattern {const char*} glob
2015-04-12 21:37:17 +08:00
& the matching pattern with glob style, only effective if not NULL
* @param count {const size_t*}
2015-04-12 21:37:17 +08:00
* limit the max number of the results stored in array, only
* effective when not NULL
* @return {int}
2015-04-12 21:37:17 +08:00
* return the next cursor value as follow:
* 0 out
*
2019-03-28 11:18:10 +08:00
* iterating is finished and the out should be checked if emtpy
* -1:
2015-04-12 21:37:17 +08:00
* some error happened
* >0: 使 out
2015-04-12 21:37:17 +08:00
* the next cursor value for iterating
*/
2015-01-23 17:23:04 +08:00
int scan(int cursor, std::vector<string>& out,
const char* pattern = NULL, const size_t* count = NULL);
2014-11-19 00:25:21 +08:00
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)