2015-04-15 19:05:58 +02:00
# pragma once
2015-04-16 14:10:44 +02:00
# ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_H_
# define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_H_
2015-04-15 19:05:58 +02:00
# include "CacheEntry.h"
2015-04-15 20:39:58 +02:00
# include "QueueMap.h"
2015-04-16 14:10:44 +02:00
# include "../../interface/Block.h"
2015-04-15 19:05:58 +02:00
# include <memory>
# include <mutex>
namespace blockstore {
2015-04-16 14:10:44 +02:00
namespace caching {
2015-04-18 16:50:19 +02:00
class PeriodicTask ;
2015-04-15 19:05:58 +02:00
2015-04-15 20:39:58 +02:00
//TODO Test
//TODO Also throw blocks out after a timeout
2015-04-15 19:05:58 +02:00
class Cache {
public :
static constexpr uint32_t MAX_ENTRIES = 1000 ;
2015-04-18 16:50:19 +02:00
//TODO Experiment with good values
static constexpr double PURGE_LIFETIME_SEC = 0.5 ; //When an entry has this age, it will be purged from the cache
static constexpr double PURGE_INTERVAL = 0.5 ; // With this interval, we check for entries to purge
static constexpr double MAX_LIFETIME_SEC = PURGE_LIFETIME_SEC + PURGE_INTERVAL ; // This is the oldest age an entry can reach (given purging works in an ideal world, i.e. with the ideal interval and in zero time)
2015-04-15 19:05:58 +02:00
Cache ( ) ;
virtual ~ Cache ( ) ;
void push ( std : : unique_ptr < Block > block ) ;
std : : unique_ptr < Block > pop ( const Key & key ) ;
private :
2015-04-18 16:50:19 +02:00
void _popOldEntries ( ) ;
2015-04-15 19:05:58 +02:00
mutable std : : mutex _mutex ;
2015-04-15 20:39:58 +02:00
QueueMap < Key , CacheEntry > _cachedBlocks ;
2015-04-18 16:50:19 +02:00
std : : unique_ptr < PeriodicTask > _timeoutFlusher ;
2015-04-15 19:05:58 +02:00
} ;
}
}
# endif