64,683
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include <string>
using namespace std;
class RedisBase
{
public:
virtual ~RedisBase() {};
virtual void Init() = 0;
virtual void Init(char* ip, int port) = 0;
virtual char* Command(char* cmd, bool needresult = false) = 0;
};
extern "C" _declspec(dllexport) RedisBase* GetInstance(); //错误在这一行
#pragma once
#include "stdafx.h"
#include "hiredis/hiredis.h"
#define NO_QFORKIMPL //这一行必须加才能正常使用
#include "Win32_Interop\win32fixes.h"
#pragma comment(lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib")
using namespace std;
class RedisBase
{
public:
virtual ~RedisBase() {};
virtual void Init() = 0;
virtual void Init(char* ip, int port) = 0;
virtual char* Command(char* cmd, bool needresult = false) = 0;
};
extern "C" _declspec(dllexport) RedisBase* GetInstance();
// redis.cpp : 定义 DLL 应用程序的导出函数。
//
#include "redis.h"
#include <iostream>
#include "atomic_lock.h"
using namespace std;
class Redis :public RedisBase
{
private:
redisContext *context;
Atomic_Lock lock;
public:
virtual void Init()
{
context = redisConnect("127.0.0.1", 6379);
redisEnableKeepAlive(context);
if (context->err) {
printf("Connection error: %s\n", context->errstr);
exit(1);
}
}
virtual void Init(char* ip, int port)
{
context = redisConnect(ip, port);
redisEnableKeepAlive(context);
if (context->err) {
printf("Connection error: %s\n", context->errstr);
exit(1);
}
}
virtual ~Redis()
{
redisFreeKeepFd(context);
redisFree(context);
}
virtual char* Command(char* cmd, bool needresult = false)
{
lock.Lock();
redisReply* reply = (redisReply *)redisCommand(context, cmd);
if (!needresult)
{
freeReplyObject(reply);
lock.UnLock();
return NULL;
}
char* result = NULL;
if (reply->len > 0)
{
result = new char[reply->len + 1];
memcpy(result, reply->str, reply->len);
result[reply->len] = '\0';
}
freeReplyObject(reply);
lock.UnLock();
return result;
}
};
RedisBase* GetInstance()
{
return new Redis();
}
RedisBase* GetInstance()
{
return new Redis; // 去掉括号。
}