2015-04-15 15:46:35 +02:00
|
|
|
#pragma once
|
2015-10-15 13:09:21 +02:00
|
|
|
#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_CACHEENTRY_H_
|
|
|
|
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_CACHEENTRY_H_
|
2015-04-15 15:46:35 +02:00
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
|
|
|
#include <messmer/cpp-utils/macros.h>
|
2015-04-18 16:50:19 +02:00
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
2015-04-15 15:46:35 +02:00
|
|
|
|
|
|
|
namespace blockstore {
|
2015-04-16 14:10:44 +02:00
|
|
|
namespace caching {
|
2015-04-15 15:46:35 +02:00
|
|
|
|
2015-04-27 22:46:57 +02:00
|
|
|
template<class Key, class Value>
|
2015-11-27 14:06:40 +01:00
|
|
|
class CacheEntry final {
|
2015-04-15 15:46:35 +02:00
|
|
|
public:
|
2015-04-27 22:46:57 +02:00
|
|
|
explicit CacheEntry(Value value): _lastAccess(currentTime()), _value(std::move(value)) {
|
2015-04-15 15:46:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 19:05:58 +02:00
|
|
|
CacheEntry(CacheEntry &&) = default;
|
|
|
|
|
|
|
|
double ageSeconds() const {
|
2015-04-18 16:50:19 +02:00
|
|
|
return ((double)(currentTime() - _lastAccess).total_nanoseconds()) / ((double)1000000000);
|
2015-04-15 15:46:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 22:46:57 +02:00
|
|
|
Value releaseValue() {
|
|
|
|
return std::move(_value);
|
2015-04-15 15:46:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-04-18 16:50:19 +02:00
|
|
|
boost::posix_time::ptime _lastAccess;
|
2015-04-27 22:46:57 +02:00
|
|
|
Value _value;
|
2015-04-15 15:46:35 +02:00
|
|
|
|
2015-04-18 16:50:19 +02:00
|
|
|
static boost::posix_time::ptime currentTime() {
|
|
|
|
return boost::posix_time::microsec_clock::local_time();
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:46:35 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CacheEntry);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|