54 using multi_value_types = black_magic::S<bool, int64_t, double, std::string>;
122 using DataPair = std::pair<uint64_t , std::string >;
126 void add(std::string key, uint64_t time)
128 auto it = times_.find(key);
129 if (it != times_.end()) remove(key);
131 queue_.insert({time, std::move(key)});
134 void remove(
const std::string& key)
136 auto it = times_.find(key);
137 if (it != times_.end())
139 queue_.erase({it->second, key});
147 if (queue_.empty())
return std::numeric_limits<uint64_t>::max();
148 return queue_.begin()->first;
151 std::string pop_first()
153 auto it = times_.find(queue_.begin()->second);
154 auto key = it->first;
156 queue_.erase(queue_.begin());
160 using iterator =
typename std::set<DataPair>::const_iterator;
162 iterator begin()
const {
return queue_.cbegin(); }
164 iterator end()
const {
return queue_.cend(); }
167 std::set<DataPair> queue_;
168 std::unordered_map<std::string, uint64_t> times_;
194 using lock = std::scoped_lock<std::mutex>;
195 using rc_lock = std::scoped_lock<std::recursive_mutex>;
200 std::recursive_mutex& mutex()
207 bool exists() {
return bool(node); }
211 auto get(
const std::string& key,
const F& fallback = F())
217 ->
decltype(std::declval<session::multi_value>().get<F>(std::declval<F>()))
219 if (!node)
return fallback;
220 rc_lock l(node->mutex);
222 auto it = node->entries.find(key);
223 if (it != node->entries.end())
return it->second.get<F>(fallback);
229 void set(
const std::string& key, T value)
232 rc_lock l(node->mutex);
234 node->dirty.insert(key);
235 node->entries[key].set(std::move(value));
238 bool contains(
const std::string& key)
240 if (!node)
return false;
241 return node->entries.find(key) != node->entries.end();
245 template<
typename Func>
246 void apply(
const std::string& key,
const Func& f)
248 using traits = utility::function_traits<Func>;
249 using arg =
typename std::decay<typename traits::template arg<0>>::type;
250 using retv =
typename std::decay<typename traits::result_type>::type;
252 rc_lock l(node->mutex);
253 node->dirty.insert(key);
254 node->entries[key].set<retv>(f(node->entries[key].get(arg{})));
258 void remove(
const std::string& key)
261 rc_lock l(node->mutex);
262 node->dirty.insert(key);
263 node->entries.erase(key);
267 std::string string(
const std::string& key)
269 if (!node)
return "";
270 rc_lock l(node->mutex);
272 auto it = node->entries.find(key);
273 if (it != node->entries.end())
return it->second.string();
278 std::vector<std::string> keys()
280 if (!node)
return {};
281 rc_lock l(node->mutex);
283 std::vector<std::string> out;
284 for (
const auto& p : node->entries)
285 out.push_back(p.first);
291 void refresh_expiration()
294 node->requested_refresh =
true;
302 if (!node) node = std::make_shared<session::CachedSession>();
305 std::shared_ptr<session::CachedSession> node;
308 template<
typename... Ts>
313 id_length_(id_length),
315 store_(std::forward<Ts>(ts)...), mutex_(new std::mutex{})
318 template<
typename... Ts>
321 CookieParser::Cookie(
"session").path(
"/").max_age( 30 * 24 * 60 * 60),
323 std::forward<Ts>(ts)...)
326 template<
typename AllContext>
327 void before_handle(request& , response& , context& ctx, AllContext& all_ctx)
331 auto& cookies = all_ctx.template get<CookieParser>();
332 auto session_id = load_id(cookies);
333 if (session_id ==
"")
return;
336 auto it = cache_.find(session_id);
337 if (it != cache_.end())
339 it->second->referrers++;
340 ctx.node = it->second;
345 if (!store_.contains(session_id))
return;
347 auto node = std::make_shared<session::CachedSession>();
348 node->session_id = session_id;
357 CROW_LOG_ERROR <<
"Exception occurred during session load";
362 cache_[session_id] = node;
365 template<
typename AllContext>
366 void after_handle(request& , response& , context& ctx, AllContext& all_ctx)
369 if (!ctx.node || --ctx.node->referrers > 0)
return;
370 ctx.node->requested_refresh |= ctx.node->session_id ==
"";
373 if (ctx.node->session_id ==
"")
376 ctx.node->session_id = std::move(ctx.node->requested_session_id);
377 if (ctx.node->session_id ==
"")
379 ctx.node->session_id = utility::random_alphanum(id_length_);
384 cache_.erase(ctx.node->session_id);
387 if (ctx.node->requested_refresh)
389 auto& cookies = all_ctx.template get<CookieParser>();
390 store_id(cookies, ctx.node->session_id);
395 store_.save(*ctx.node);
399 CROW_LOG_ERROR <<
"Exception occurred during session save";
405 std::string next_id()
410 id = utility::random_alphanum(id_length_);
411 }
while (store_.contains(
id));
415 std::string load_id(
const CookieParser::context& cookies)
417 return cookies.get_cookie(cookie_.name());
420 void store_id(CookieParser::context& cookies,
const std::string& session_id)
422 cookie_.value(session_id);
423 cookies.set_cookie(cookie_);
430 CookieParser::Cookie cookie_;
435 std::unique_ptr<std::mutex> mutex_;
436 std::unordered_map<std::string, std::shared_ptr<session::CachedSession>> cache_;
469 FileStore(
const std::string& folder, uint64_t expiration_seconds = 30 * 24 * 60 * 60):
470 path_(folder), expiration_seconds_(expiration_seconds)
472 std::ifstream ifs(get_filename(
".expirations",
false));
474 auto current_ts = chrono_time();
477 while (ifs >> key >> time)
479 if (current_ts > time)
483 else if (contains(key))
485 expirations_.
add(key, time);
492 std::ofstream ofs(get_filename(
".expirations",
false), std::ios::trunc);
493 for (
const auto& p : expirations_)
494 ofs << p.second <<
" " << p.first <<
"\n";
499 void handle_expired()
502 auto current_ts = chrono_time();
503 while (current_ts > expirations_.
peek_first() && deleted < 3)
505 evict(expirations_.pop_first());
514 std::ifstream file(get_filename(cn.session_id));
516 std::stringstream buffer;
517 buffer << file.rdbuf() << std::endl;
519 for (
const auto& p : json::load(buffer.str()))
520 cn.entries[p.key()] = session::multi_value::from_json(p);
525 if (cn.requested_refresh)
526 expirations_.
add(cn.session_id, chrono_time() + expiration_seconds_);
527 if (cn.dirty.empty())
return;
529 std::ofstream file(get_filename(cn.session_id));
531 for (
const auto& p : cn.entries)
532 jw[p.first] = p.second.json();
533 file << jw.dump() << std::flush;
536 std::string get_filename(
const std::string& key,
bool suffix =
true)
538 return utility::join_path(path_, key + (suffix ?
".json" :
""));
541 bool contains(
const std::string& key)
543 std::ifstream file(get_filename(key));
547 void evict(
const std::string& key)
549 std::remove(get_filename(key).c_str());
552 uint64_t chrono_time()
const
554 return std::chrono::duration_cast<std::chrono::seconds>(
555 std::chrono::system_clock::now().time_since_epoch())
560 uint64_t expiration_seconds_;