| 179 | }; |
| 180 | |
| 181 | std::shared_ptr<ExtendedPersistentCache> ExtendedPersistentCache::make_from_config( |
| 182 | std::string type, std::unordered_map<std::string, std::string> args, |
| 183 | std::string& err_msg) { |
| 184 | try { |
| 185 | if (type == "redis") { |
| 186 | std::string prefix = args.at("prefix"); |
| 187 | std::optional<std::string> password = args.count("password") |
| 188 | ? args.at("password") |
| 189 | : std::optional<std::string>(); |
| 190 | auto cache = std::make_shared<RedisCache>(prefix, 100); |
| 191 | if (args.count("unixsocket")) { |
| 192 | std::string unixsocket = args.at("unixsocket"); |
| 193 | cache->connect(unixsocket, 0, password); |
| 194 | } else { |
| 195 | std::string ip = args.at("hostname"); |
| 196 | int port = atoi(args.at("port").c_str()); |
| 197 | std::optional<std::string> password = |
| 198 | args.count("password") ? args.at("password") |
| 199 | : std::optional<std::string>(); |
| 200 | cache->connect(ip, port, password); |
| 201 | } |
| 202 | return cache; |
| 203 | } else if (type == "in-file") { |
| 204 | std::string path = args.at("path"); |
| 205 | auto cache = std::make_shared<ExtendedInFilePersistentCache>(); |
| 206 | cache->open(path); |
| 207 | return cache; |
| 208 | } else if (type == "in-memory") { |
| 209 | auto cache = std::make_shared<ExtendedInFilePersistentCache>(); |
| 210 | cache->open(); |
| 211 | return cache; |
| 212 | } else { |
| 213 | mgb_assert(false, "persistent cache type %s unsupported", type.c_str()); |
| 214 | } |
| 215 | } catch (const std::exception& exc) { |
| 216 | err_msg = exc.what(); |
| 217 | } catch (...) { |
| 218 | err_msg = "unknown exception"; |
| 219 | } |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
| 223 | } // namespace mgb::imperative::persistent_cache |
| 224 | |