Skip to content

Instantly share code, notes, and snippets.

@tiijima
Last active October 16, 2023 04:27
Show Gist options
  • Save tiijima/14152256264224862e93 to your computer and use it in GitHub Desktop.
Save tiijima/14152256264224862e93 to your computer and use it in GitHub Desktop.
RedisをC言語(C++) から利用する

RedisをC言語(C++) から利用する

参考リンク

Redisインストール

CentOS 6

インストール

wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
sudo yum -y update
sudo yum -y install redis

C言語から利用するライブラリをインストール

sudo yum install hiredis hiredis-devel -y

staticlinkする場合は自前でmake

mkdir ~/src
cd ~/src
git clone -b v0.13.0 https://github.com/redis/hiredis.git
cd hiredis
make
sudo make install

redisサーバー起動

sudo /etc/init.d/redis start

mac

インストール

brew install redis

C言語から利用するライブラリをインストール

brew install hiredis

redisサーバー起動

redis-server /usr/local/etc/redis.conf

c++ sample

main.cpp

#include <stdio.h>
#include <hiredis.h>
#include <assert.h>

void keyValueTest(redisContext* c)
{
	// 参考:[文字列型 — redis 2.0.3 documentation](http://redis.shibu.jp/commandreference/strings.html)

	redisReply* setReply = (redisReply *)redisCommand(c,"SET key001 value001");
	if(setReply){ 
		freeReplyObject(setReply);
	}

	redisReply* getReply = (redisReply *)redisCommand(c , "GET key001");
	printf("KeyValueTest[aaa : %s]\n", getReply->str);
	if(getReply){ 
		freeReplyObject(getReply);
	}

	redisReply* delReply = (redisReply *)redisCommand(c , "DEL key001");
	if(delReply){ 
		freeReplyObject(delReply);
	}
}

///////////////////////////////////////////////////////////////
// これ以降サンプルはredisCommandの戻り値をfreeしていないので
// コピペするときは注意してくださいな
///////////////////////////////////////////////////////////////
void hashTest(redisContext* c)
{
	// 参考:[ハッシュ型 — redis 2.0.3 documentation](http://redis.shibu.jp/commandreference/hashes.html)

	const char* KEY = "key001";

	redisCommand(c,"HSET %s field001 1111", KEY);
	redisCommand(c,"HMSET %s field002 1234567890 field003 9876543210", KEY);

	redisReply* reply = NULL;
	reply = (redisReply *)redisCommand(c, "HLEN %s", KEY);
	printf("HashTest[HLEN : %lld]\n", reply->integer);

	reply = (redisReply *)redisCommand(c, "HGET %s field001", KEY);
	printf("HashTest[HGET : field001:%s]\n", reply->str);
	reply = (redisReply *)redisCommand(c, "HGET %s field002", KEY);
	printf("HashTest[HGET : field002:%s]\n", reply->str);
	reply = (redisReply *)redisCommand(c, "HGET %s field003", KEY);
	printf("HashTest[HGET : field003:%s]\n", reply->str);

	// reply->element[0]がkey, reply->element[1]がvalueというような形で取り出せる
	reply = (redisReply *)redisCommand(c, "HGETALL %s", KEY);
	for (int i = 0; i < reply->elements; i+=2) {
		printf("HashTest[HGETALL key:%s, field:%s, value:%s]\n", KEY, reply->element[i]->str, reply->element[i+1]->str);
	}

	redisCommand(c , "DEL %s", KEY);


	// key, field, valueにスペースを混ぜる場合には以下のようにフォーマット指定子を利用すれば良い
	const char* KEY2 = "key 002";
	redisCommand(c,"HMSET %s %s %s", KEY2, "field 004", "aaa bbb");
	reply = (redisReply *)redisCommand(c, "HGET %s %s", KEY2, "field 004");
	printf("HashTest[HGET : field 004:%s]\n", reply->str);
	redisCommand(c , "DEL %s", KEY2);
}


int main()
{
	redisContext* c = redisConnect("127.0.0.1", 6379);
	if (c != NULL && c->err) {
		printf("Error: %s\n", c->errstr);
		return 1;
	}

	keyValueTest(c);
	hashTest(c);

	redisFree(c);

	return 0;
}

Makefile(centos6)

PROGRAM = test
CPPFLAGS = -g -Wall -std=c++0x \
	-I/usr/include \
	-I/usr/include/hiredis \
	-I/usr/local/include/
LDLIBS = \
	-L/usr/lib \
	-L/usr/lib64 \
	-L/usr/local/lib
SRC = \
	main.cpp
OBJS = $(SRC:.cpp=.o)
LDFLAGS = \
	-lhiredis \
	-lstdc++
CXX = g++

all : $(PROGRAM)

$(PROGRAM) : $(OBJS)
	$(CXX) $(CPPFLAGS) -o $(PROGRAM) $(OBJS) $(LDLIBS) $(LDFLAGS)

clean :
	rm -vf *.o $(PROGRAM)

Makefile staticlink用(centos6)

PROGRAM = test
CPPFLAGS = -g -Wall -std=c++0x \
	-I/usr/local/include/hiredis
LDLIBS = \
	-L/usr/local/lib
SRC = \
	main.cpp
OBJS = $(SRC:.cpp=.o)
LDFLAGS = \
	-lhiredis \
	-lstdc++
CXX = g++

all : $(PROGRAM)

$(PROGRAM) : $(OBJS)
	$(CXX) $(CPPFLAGS) -static -o $(PROGRAM) $(OBJS) $(LDLIBS) $(LDFLAGS)

clean :
	rm -vf *.o $(PROGRAM)

Makefile(mac)

PROGRAM = test
CPPFLAGS = -g -Wall -std=c++0x \
	-I/usr/include \
	-I/usr/local/Cellar/hiredis/0.13.3/include/hiredis \
	-I/usr/local/include/
LDLIBS = \
	-L/usr/lib \
	-I/usr/local/Cellar/hiredis/0.13.3/lib \
	-L/usr/local/lib
SRC = \
	main.cpp
OBJS = $(SRC:.cpp=.o)
LDFLAGS = \
	-lhiredis \
	-lstdc++
CXX = g++

all : $(PROGRAM)

$(PROGRAM) : $(OBJS)
	$(CXX) $(CPPFLAGS) -o $(PROGRAM) $(OBJS) $(LDLIBS) $(LDFLAGS)

clean :
	rm -vf *.o $(PROGRAM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment