C++ 中的缓存:Redis、Memcached 与内嵌解决方案

面向初学者的在线Python训练器

轻松学习Python,无需理论负担。在浏览器中直接解决带自动检查的实践任务,无需安装任何东西。

开始课程
```html

引言:为什么需要在 C++ 中使用缓存?

在现代 C++ 开发中,应用程序的运行速度通常是一个关键因素。缓存可以显著减少数据访问时间,降低数据库负载,并加速重复计算操作的执行。在本文中,我们将详细探讨 C++ 中三种主要的缓存方法:使用外部系统(Redis 和 Memcached)以及开箱即用的内置解决方案。

无论您是在开发高负载 Web 服务器、游戏引擎还是金融系统,选择正确的缓存策略都可以将性能提升数十倍。我们将分析每种方法的优缺点,并提供现成的代码示例以便快速集成。

1. C++ 中的内置缓存解决方案

在连接外部服务之前,值得考虑 C++ 语言本身及其标准库提供的功能。内置解决方案非常适合单线程应用程序,或者数据不需要在多个进程之间共享的情况。

1.1. 基于 std::unordered_map 的简单 LRU 缓存

LRU(最近最少使用)算法是内置缓存中最流行的算法之一。当缓存达到内存限制时,它会自动删除最近最少使用的元素。

#include <unordered_map>
#include <list>
#include <mutex>

template <typename Key, typename Value>
class LRUCache {
private:
    size_t capacity_;
    std::list<Key> items_;
    std::unordered_map<Key, std::pair<Value, typename std::list<Key>::iterator>> cache_;
    std::mutex mutex_;

public:
    LRUCache(size_t capacity) : capacity_(capacity) {}

    void put(const Key& key, const Value& value) {
        std::lock_guard<std::mutex> lock(mutex_);
        auto it = cache_.find(key);
        if (it != cache_.end()) {
            items_.erase(it->second.second);
            cache_.erase(it);
        }
        items_.push_front(key);
        cache_[key] = {value, items_.begin()};
        
        if (cache_.size() > capacity_) {
            auto last = items_.back();
            items_.pop_back();
            cache_.erase(last);
        }
    }

    std::optional<Value> get(const Key& key) {
        std::lock_guard<std::mutex> lock(mutex_);
        auto it = cache_.find(key);
        if (it == cache_.end()) return std::nullopt;
        
        // 将键移动到列表开头
        items_.splice(items_.begin(), items_, it->second.second);
        return it->second.first;
    }
};

优点: 开销最小,无网络延迟,完全控制数据。

缺点: 数据不在线程之间共享(需要手动同步),受限于单个进程的内存。

1.2. 使用 std::shared_mutex 实现多线程

对于多线程应用程序,可以使用 shared_mutex 来提高性能,它允许多个线程同时读取:

#include <shared_mutex>

class ThreadSafeCache {
    std::map<std::string, std::string> data_;
    mutable std::shared_mutex mutex_;
    
public:
    std::string read(const std::string& key) {
        std::shared_lock lock(mutex_);
        auto it = data_.find(key);
        return (it != data_.end()) ? it->second : "";
    }
    
    void write(const std::string& key, const std::string& value) {
        std::unique_lock lock(mutex_);
        data_[key] = value;
    }
};

2. 在 C++ 中使用 Memcached 进行缓存

Memcached 是一个分布式内存缓存系统,非常适合存储小型数据片段

```

博客

书籍推荐