File: RedisCache.h

package info (click to toggle)
drogon 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,096 kB
  • sloc: cpp: 52,222; sh: 249; xml: 20; makefile: 11
file content (28 lines) | stat: -rw-r--r-- 767 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma once
#include <drogon/utils/coroutine.h>
#include <drogon/nosql/RedisClient.h>

template <typename T>
T fromString(const std::string &);

template <typename T>
std::string toString(const T &);

template <typename T>
drogon::Task<T> getFromCache(
    const std::string &key,
    const drogon::nosql::RedisClientPtr &client) noexcept(false)
{
    auto value = co_await client->execCommandCoro("get %s", key.data());
    co_return fromString<T>(value.asString());
}

template <typename T>
drogon::Task<> updateCache(
    const std::string &key,
    T &&value,
    const drogon::nosql::RedisClientPtr &client) noexcept(false)
{
    std::string vstr = toString(std::forward<T>(value));
    co_await client->execCommandCoro("set %s %s", key.data(), vstr.data());
}