/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include "base/last_used_cache.h" namespace Core { template class MediaActiveCache { public: template MediaActiveCache(int64 limit, Unload &&unload); void up(Type *entry); void remove(Type *entry); void clear(); void increment(int64 amount); void decrement(int64 amount); private: template void check(Unload &&unload); base::last_used_cache _cache; SingleQueuedInvokation _delayed; int64 _usage = 0; int64 _limit = 0; }; template template MediaActiveCache::MediaActiveCache(int64 limit, Unload &&unload) : _delayed([=] { check(unload); }) , _limit(limit) { } template void MediaActiveCache::up(Type *entry) { _cache.up(entry); _delayed.call(); } template void MediaActiveCache::remove(Type *entry) { _cache.remove(entry); } template void MediaActiveCache::clear() { _cache.clear(); } template void MediaActiveCache::increment(int64 amount) { _usage += amount; } template void MediaActiveCache::decrement(int64 amount) { _usage -= amount; } template template void MediaActiveCache::check(Unload &&unload) { while (_usage > _limit) { if (const auto entry = _cache.take_lowest()) { unload(entry); } else { break; } } } } // namespace Core