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
core::PersistentStringCache::Data
Simple pair of value and metadata.
Definition: persistent_string_cache.h:79
core::CacheEvent::touch
An entry was refreshed by a call to touch().
optional.h
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::CacheEvent
CacheEvent
Event types that can be monitored.
Definition: cache_events.h:38
core
Top-level namespace for core functionality.
Definition: cache_codec.h:23
std
STL namespace.
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
core::PersistentStringCache::Data::value
std::string value
Stores the value of an entry.
Definition: persistent_string_cache.h:84
core::CacheEvent::put
An entry was added by a call to put() or get_or_put().
core::PersistentStringCache
A cache of key-value pairs with persistent storage.
Definition: persistent_string_cache.h:68
core::CacheEvent::invalidate
An entry was removed by a call to invalidate(), take(), or take_data().
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::PersistentStringCache::UPtr
std::unique_ptr< PersistentStringCache > UPtr
Definition: persistent_string_cache.h:74
cache_discard_policy.h
core::Optional
boost::optional< T > Optional
Convenience typedef for nullable values.
Definition: optional.h:33
persistent_cache_stats.h
cache_events.h
core::CacheDiscardPolicy
CacheDiscardPolicy
Indicates the discard policy to make room for entries when the cache is full.
Definition: cache_discard_policy.h:35
core::PersistentCacheStats
Class that provides (read-only) access to cache statistics and settings.
Definition: persistent_cache_stats.h:42