Caching in C++: Redis and Other Tools
Caching is one of the key techniques for optimizing the performance of C++ applications. It reduces data access time and lowers the load on databases and external services. In this article, we will explore the main libraries and tools for implementing caching in C++: from industrial solutions based on Redis and Memcached to lightweight built-in implementations.
1. Redis and its C++ Clients
Redis is a high-performance in-memory key-value data store, often used as a cache. Two main libraries are available for working with Redis from C++:
1.1. hiredis
hiredis is a minimalistic C library officially supported by Redis. It provides both synchronous and asynchronous APIs.
Installation:
# Ubuntu/Debiansudo apt install libhiredis-dev
# macOSbrew install hiredis
# Build from sourcegit clone https://github.com/redis/hiredis.gitcd hiredis && make && sudo make installUsage example (synchronous mode):
#include <hiredis/hiredis.h>#include <iostream>
int main() { redisContext *c = redisConnect("127.0.0.1", 6379); if (c == nullptr || c->err) { std::cerr << "Connection error: " << c->errstr << std::endl; return 1; }
// Set value redisReply *reply = (redisReply*)redisCommand(c, "SET key1 value1"); freeReplyObject(reply);
// Get value reply = (redisReply*)redisCommand(c, "GET key1"); if (reply->type == REDIS_REPLY_STRING) { std::cout << "Value: " << reply->str << std::endl; } freeReplyObject(reply);
redisFree(c); return 0;}1.2. redis-plus-plus
redis-plus-plus is a modern C++17 wrapper over hiredis with a convenient interface, support for connection pools, transactions, and pub/sub.
Installation:
git clone https://github.com/sewenew/redis-plus-plus.gitcd redis-plus-plusmkdir build && cd buildcmake -DREDIS_PLUS_PLUS_CXX_STANDARD=17 ..make && sudo make installExample:
#include <sw/redis++/redis++.h>#include <iostream>
int main() { sw::redis::Redis redis("tcp://127.0.0.1:6379");
// Set with TTL (time-to-live 60 seconds) redis.setex("session:user1", 60, "active");
// Get auto val = redis.get("session:user1"); if (val) { std::cout << "Session: " << *val << std::endl; }
// Check existence bool exists = redis.exists("session:user1"); std::cout << "Exists: " << std::boolalpha << exists << std::endl;
return 0;}2. Memcached and libmemcached
Memcached is another popular in-memory store, optimized for caching small objects. The main C++ library is libmemcached.
Installation:
sudo apt install libmemcached-dev
# Build from sourcegit clone https://github.com/awesomized/libmemcached.gitcd libmemcached && ./configure && make && sudo make installExample:
#include <libmemcached/memcached.h>#include <iostream>#include <cstring>
int main() { memcached_st *memc = memcached_create(nullptr); memcached_server_st *servers = memcached_server_list_append(nullptr, "127.0.0.1", 11211, nullptr); memcached_server_push(memc, servers); memcached_server_list_free(servers);
// Write const char *key = "cache_key"; const char *value = "cached_value"; memcached_return_t rc = memcached_set(memc, k