Redis HASH操作タイプパッケージ

8174 ワード

以下は私のプロジェクトで使用する、redisがhashテーブルを操作する際のクラスであり、hashに対する操作をすべて自己カプセル化している.テストに合格する
ヘッダファイル
#pragma once

class redisOperator
{

private:	
	//    redis      
	redisContext *c;
public:	
	redisOperator(void);

	//HDELL   key         ,         (      num   )
	int hashHdel(string& key,const int& num,const char** value);
	
	//HEXISTS              ,    1    0
	int hashHexists(const string& key,const string& field);
	
	//HGET       key      field   。        ,  NULL
	string hashHget(const string& key,const string& field);
	
	//HGETALL key       key  ,      。
	//    true   false
	string hashHgetall(const string& key);
	
	//HINCRYBY      field       
	//   :field   (      ,      log )
	int hashHincrby(const string& key,const string& field,unsigned int incr);

	//HINCRBYFLOAT       field       incr
	//   :field   (        log )
	string hashHincrbyfloat(const string& key,const string& field ,float incr);

	//HKEYS      key     
	//     NULL
	string hashHkeys(const string& key);

	//HLEN           
	//     0 
	int hashHlen(const string& key);

	//HMGET      key           
	//   :                 ,       NULL
	string hashHmset(const string& key,const int& num,const char**argc);

	//HMSET      field-value       key 
	void hashHmset(string& key,int num,char** field,char**value);

	//HSET     key  field     value
	//  1      ,  0         
	bool hashHset(string& key,string& field ,const string& value);

	//HSETNX     key  field     value
	//    1    0
	bool hashHsetnx(string& key,string& field ,const string& value); 
	
	//HVALS      key      
	//key      
	string hashHvals(const string& key);

	//    
	virtual ~redisOperator(void);
private:
	redisOperator(const redisOperator&);
	redisOperator& redisOpeartor=(const redisOperator&);
};

インプリメンテーションファイル
#include "redisOperator.h"

redisOperator::redisOperator(void)
{
	const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
	int port = (argc > 2) ? atoi(argv[2]) : 6379;
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
	c = redisConnectWithTimeout(hostname, port, timeout);
	cacheHandle handle(c,hostname);
	if (c == NULL || c->err)
	{
		if (c)
		{
			cout<<"Connection error: %s
", c->errstr; redisFree(c); } else { cout<<"Connection error: can't allocate redis context
"; } exit(-1); } } //HDELL key , ( num ) int redisOperator::hashHdel(string& key,const int& num,const char** value) { int tmp = 0; redisReply *reply; for(int i= 1 ;i < num;i++) { reply = (redisReply*)redisCommand(c,"HDEL %s %s",key.c_str(),value[i]); if(reply->type == REDIS_REPLY_INTEGER) tmp += teply -> integer; } freeReplyObject(reply); return tmp; } //HEXISTS , 1 0 int hashHexists(const string& key,const string& field) { int tmp = 0; redisReply *reply; reply = (redisReply*)redisCommand(c,"HEXISTS %s %s",key.c_str(),field.c_str()); if(reply->type == REDIS_REPLY_INTEGER) tmp = reply->integer; freeReplyObject(reply); return tmp; } //HGET key field 。 , NULL string redisOperator::hashHget(const string& key,const string& field) { string tmp(""); redisReply *reply; reply = (redisReply*)redisCommand(c,"HGET %s %s",key.c_str(),field.c_str()); if(reply->type == REDIS_REPLY_STRING) tmp = reply->str; freeReplyObject(reply); return tmp; } //HGETALL key key , 。 // true false string redisOperator::hashHgetall(const string& key) { string tmp(""); redisReply *reply; reply = (redisReply*)redisCommand(c,"HGETALL %s ",key.c_str()); if(reply->type == REDIS_REPLY_ARRAY) { for(int j = 0 ; j< reply->elements;j++) { tmp += reply->element[j]->str; tmp += "
"; tmp += hashHget(key,reply->element[j]->str); tmp += "
"; j++; } freeReplyObject(reply); tmp=tmp.substr(0,tmp.length()-1); } else return tmp; } //HINCRYBY field // :field ( -1, log ) int redisOperator::hashHincrby(const string& key,const string& field,unsigned int incr) { int tmp = -1; redisReply *reply; reply = (redisReply*)redisCommand(c,"HINCRBY %s %s %d",key.c_str(),field.c_str(),incr); if(reply->type == REDIS_REPLY_INTEGER) { tmp=reply->integer; } else freeReplyObject(reply); return tmp; } //HINCRBYFLOAT field incr // :field ( log ) string redisOperator::hashHincrbyfloat(const string& key,const string& field ,float incr) { string tmp(""); reply = (redisReply*)redisCommand(c,"HINCRBYFLOAT %s %s %f",key.c_str(),field.c_str(),incr); if(reply->type == REDIS_REPLY_STRING) { tmp=reply->str; } freeReplyObject(reply); return tmp; } //HKEYS key // NULL string redisOperator::hashHkeys(const string& key) { string tmp(""); redisReply *reply; reply = (redisReply*)redisCommand(c,"HKEYS %s ",key.c_str()); if(reply->type == REDIS_REPLY_ARRAY) { for(int j = 0 ; j< reply->elements;j++) { tmp += reply->element[j]->str; tmp += "
"; } tmp=tmp.substr(0,tmp.length()-1); } return tmp; } //HLEN // 0 int redisOperator::hashHlen(const string& key) { int tmp = 0; redisReply *reply; reply = (redisReply*)redisCommand(c,"HLEN %s ",key.c_str()); if(reply->type == REDIS_REPLY_INTEGER) { tmp = reply->integer; } return tmp; } //HMGET key // : , string redisOperator::hashHmget(const string& key,const int& num,const char**field) { string tmp(""); for(int i = 0;i< num;i++) { reply = (redisReply*)redisCommand(c,"HGET %s %s",key.c_str(),field[i]); if(reply->type == REDIS_REPLY_STRING) { tmp += reply->str; tmp += "
"; } freeReplyObject(reply); } tmp = tmp.substr(0,tmp.length()-1); return tmp; } //HMSET field-value key void redisOperator::hashHmset(string& key,int num,char**field,char**value) { redisReply *reply; for(int i= 0 ;i < num;i++) { reply = (redisReply*)redisCommand(c,"HSET %s %s %s",key.c_str(),field[i],value[i]); if(reply->type == REDIS_REPLY_ERROR) { // cout<<"an err occur"<<endl; abort(); } freeReplyObject(reply); } } //HSET key field value // 1 , 0 bool redisOperator::redisOperator::hashHset(string& key,string& field ,const string& value) { int tmp=0; redisReply *reply; reply = (redisReply*)redisCommand(c,"HSET %s %s %s",key.c_str(),field.c_str(),value.c_str()); if(reply->type == REDIS_REPLY_INTEGER) tmp += reply -> integer; freeReplyObject(reply); return tmp!=0?true:false; } //HSETNX key field value // 1 0 bool redisOperator::hashHsetnx(string& key,string& field ,const string& value) { int tmp=0; redisReply *reply; reply = (redisReply*)redisCommand(c,"HSETNX %s %s %s",key.c_str(),field.c_str(),value.c_str()); if(reply->type == REDIS_REPLY_INTEGER) tmp += reply -> integer; freeReplyObject(reply); return (tmp!=0?true:false); } //HVALS key //key //NOTE string redisOperator::hashHvals(const string& key) { string tmp(""); redisReply *reply; reply = (redisReply*)redisCommand(c,"HGETALL %s ",key.c_str()); if(reply->type == REDIS_REPLY_ARRAY) { for(int j = 0 ; j< reply->elements;j++) { tmp += hashHget(key,reply->element[j]->str); tmp += "
"; j++; } freeReplyObject(reply); tmp=tmp.substr(0,tmp.length()-1); } return tmp; } // virtual redisOperator::~redisOperator(void) { redisFree(c); } redisOperator::redisOperator(const redisOperator&) { } redisOperator& redisOperator::redisOpeartor=(const redisOperator&) { }