persistent_string_cache.h
Go to the documentation of this file.
/*
* Copyright (C) 2015 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Michi Henning <michi.henning@canonical.com>
*/
#pragma once
#include <core/cache_discard_policy.h>
#include <core/cache_events.h>
#include <core/optional.h>
#include <core/persistent_cache_stats.h>
namespace core
{
namespace internal
{
class PersistentStringCacheImpl;
} // namespace internal
class PersistentStringCache
{
public:
typedef std::unique_ptr<PersistentStringCache> UPtr;
struct Data
{
std::string value;
std::string metadata;
};
//{@
PersistentStringCache(PersistentStringCache const&) = delete;
PersistentStringCache& operator=(PersistentStringCache const&) = delete;
PersistentStringCache(PersistentStringCache&&);
PersistentStringCache& operator=(PersistentStringCache&&);
~PersistentStringCache();
//{@
static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy);
static UPtr open(std::string const& cache_path);
//{@
Optional<std::string> get(std::string const& key) const;
Optional<Data> get_data(std::string const& key) const;
Optional<std::string> get_metadata(std::string const& key) const;
bool contains_key(std::string const& key) const;
int64_t size() const noexcept;
int64_t size_in_bytes() const noexcept;
int64_t max_size_in_bytes() const noexcept;
int64_t disk_size_in_bytes() const;
CacheDiscardPolicy discard_policy() const noexcept;
PersistentCacheStats stats() const;
//{@
bool put(std::string const& key,
std::string const& value,
std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point());
bool put(std::string const& key,
char const* value,
int64_t size,
std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point());
bool put(std::string const& key,
std::string const& value,
std::string const& metadata,
std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point());
bool put(std::string const& key,
char const* value,
int64_t value_size,
char const* metadata,
int64_t metadata_size,
std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point());
typedef std::function<void(std::string const& key, PersistentStringCache& cache)> Loader;
Optional<std::string> get_or_put(std::string const& key, Loader const& load_func);
Optional<Data> get_or_put_data(std::string const& key, Loader const& load_func);
bool put_metadata(std::string const& key, std::string const& metadata);
bool put_metadata(std::string const& key, char const* metadata, int64_t size);
Optional<std::string> take(std::string const& key);
Optional<Data> take_data(std::string const& key);
bool invalidate(std::string const& key);
void invalidate(std::vector<std::string> const& keys);
template<typename It>
void invalidate(It begin, It end)
{
std::vector<std::string> keys;
while (begin < end)
{
keys.push_back(*begin++);
}
invalidate(keys);
}
void invalidate(std::initializer_list<std::string> const& keys);
void invalidate();
bool touch(
std::string const& key,
std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point());
void clear_stats();
void resize(int64_t size_in_bytes);
void trim_to(int64_t used_size_in_bytes);
void compact();
//{@
typedef std::function<void(std::string const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback;
void set_handler(CacheEvent events, EventCallback cb);
private:
// @cond
PersistentStringCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy);
PersistentStringCache(std::string const& cache_path);
std::unique_ptr<internal::PersistentStringCacheImpl> p_;
// @endcond
};
} // namespace core
cache_discard_policy.h
cache_events.h
core::PersistentCacheStats
Class that provides (read-only) access to cache statistics and settings.
Definition persistent_cache_stats.h:43
core::PersistentStringCache
A cache of key-value pairs with persistent storage.
Definition persistent_string_cache.h:69
core::PersistentStringCache::invalidate
void invalidate(std::initializer_list< std::string > const &keys)
Atomically removes the specified entries from the cache.
core::PersistentStringCache::clear_stats
void clear_stats()
Resets all statistics counters.
core::PersistentStringCache::stats
PersistentCacheStats stats() const
Returns statistics for the cache.
core::PersistentStringCache::discard_policy
CacheDiscardPolicy discard_policy() const noexcept
Returns the discard policy of the cache.
core::PersistentStringCache::get_data
Optional< Data > get_data(std::string const &key) const
Returns the data for an entry in the cache, provided the entry has not expired.
core::PersistentStringCache::touch
bool touch(std::string const &key, std::chrono::time_point< std::chrono::system_clock > expiry_time=std::chrono::system_clock::time_point())
Updates the access time of an entry.
core::PersistentStringCache::contains_key
bool contains_key(std::string const &key) const
Tests if an (unexpired) entry is in the cache.
core::PersistentStringCache::set_handler
void set_handler(CacheEvent events, EventCallback cb)
Installs a handler for one or more events.
core::PersistentStringCache::operator=
PersistentStringCache & operator=(PersistentStringCache const &)=delete
core::PersistentStringCache::max_size_in_bytes
int64_t max_size_in_bytes() const noexcept
Returns the maximum size of the cache in bytes.
core::PersistentStringCache::disk_size_in_bytes
int64_t disk_size_in_bytes() const
Returns an estimate of the disk space consumed by the cache.
core::PersistentStringCache::put_metadata
bool put_metadata(std::string const &key, std::string const &metadata)
Adds or replaces the metadata for an entry.
core::PersistentStringCache::resize
void resize(int64_t size_in_bytes)
Changes the maximum size of the cache.
core::PersistentStringCache::trim_to
void trim_to(int64_t used_size_in_bytes)
Expires entries.
core::PersistentStringCache::size
int64_t size() const noexcept
Returns the number of entries in the cache.
core::PersistentStringCache::take_data
Optional< Data > take_data(std::string const &key)
Removes an entry and returns its value and metadata.
core::PersistentStringCache::get
Optional< std::string > get(std::string const &key) const
Returns the value of an entry in the cache, provided the entry has not expired.
core::PersistentStringCache::size_in_bytes
int64_t size_in_bytes() const noexcept
Returns the number of bytes consumed by entries in the cache.
core::PersistentStringCache::get_or_put_data
Optional< Data > get_or_put_data(std::string const &key, Loader const &load_func)
Atomically retrieves or stores a cache entry.
core::PersistentStringCache::take
Optional< std::string > take(std::string const &key)
Removes an entry and returns its value.
core::PersistentStringCache::PersistentStringCache
PersistentStringCache(PersistentStringCache const &)=delete
core::PersistentStringCache::open
static UPtr open(std::string const &cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy)
Creates or opens a PersistentStringCache.
core::PersistentStringCache::~PersistentStringCache
~PersistentStringCache()
core::PersistentStringCache::open
static UPtr open(std::string const &cache_path)
Opens an existing PersistentStringCache.
core::PersistentStringCache::invalidate
void invalidate()
Deletes all entries from the cache.
core::PersistentStringCache::get_or_put
Optional< std::string > get_or_put(std::string const &key, Loader const &load_func)
Atomically retrieves or stores a cache entry.
core::PersistentStringCache::PersistentStringCache
PersistentStringCache(PersistentStringCache &&)
core::PersistentStringCache::get_metadata
Optional< std::string > get_metadata(std::string const &key) const
Returns the metadata for an entry in the cache, provided the entry has not expired.
core::PersistentStringCache::compact
void compact()
Compacts the database.
core::PersistentStringCache::Loader
std::function< void(std::string const &key, PersistentStringCache &cache)> Loader
Function called by the cache to load an entry after a cache miss.
Definition persistent_string_cache.h:355
core::PersistentStringCache::UPtr
std::unique_ptr< PersistentStringCache > UPtr
Definition persistent_string_cache.h:74
core::PersistentStringCache::operator=
PersistentStringCache & operator=(PersistentStringCache &&)
core::PersistentStringCache::EventCallback
std::function< void(std::string const &key, CacheEvent ev, PersistentCacheStats const &stats)> EventCallback
The type of a handler function.
Definition persistent_string_cache.h:612
core
Top-level namespace for core functionality.
Definition cache_codec.h:24
core::CacheDiscardPolicy
CacheDiscardPolicy
Indicates the discard policy to make room for entries when the cache is full.
Definition cache_discard_policy.h:36
core::CacheEvent
CacheEvent
Event types that can be monitored.
Definition cache_events.h:39
core::CacheEvent::put
@ put
An entry was added by a call to put() or get_or_put().
core::Optional
boost::optional< T > Optional
Convenience typedef for nullable values.
Definition optional.h:39
optional.h
persistent_cache_stats.h
core::PersistentStringCache::Data
Simple pair of value and metadata.
Definition persistent_string_cache.h:80
core::PersistentStringCache::Data::value
std::string value
Stores the value of an entry.
Definition persistent_string_cache.h:84
core::PersistentStringCache::Data::metadata
std::string metadata
Stores the metadata of an entry. If no metadata exists for an entry, metadata is returned as the empt...
Definition persistent_string_cache.h:90