He implementado un tipo de sonido similar que he llamado DynamicCache. Básicamente almacena los datos en una lista ordenada por la fecha de creación. Esto podría cambiarse fácilmente a la última fecha de acceso. El objetivo de mi caché es almacenar en caché los elementos de la base de datos que no cambian muy a menudo. Almacena en caché los elementos durante 5 minutos y luego los elimina para volver a leerlos cuando se acceda a ellos.
La búsqueda de caché utiliza un mapa que almacena la clave y un iterador en la lista. La búsqueda usa el mapa para buscar los datos en la lista, luego, antes de devolver el elemento, elimina todos los elementos anteriores del final de la lista. Si un artículo no está en la memoria caché, se llama a una fábrica para proporcionar los datos.
Este enfoque debe usar una lista para almacenar los datos, ya que los iteradores en el mapa siempre deben ser válidos, si utiliza un deque los iteradores podrían invalidarse después de una inserción. La lista usa una estructura para almacenar los datos, la clave, la hora en que se creó (no se accedió por última vez) y finalmente si los datos existen.
struct Record
{
KeyT key;
DataT data;
time_t createTime;
bool exists;
};
Si los datos son estáticos y se desea preservar la accedido más recientemente, entonces usted podría agregar un miembro de tiempo de acceso a la estructura, y mover el elemento a la parte superior de la lista cada vez que se accede.
Aquí está mi código, parece un poco complicado, pero esto se debe principalmente a los parámetros de la plantilla y al bloqueo del escritor lector.
#include "BWThread/BWReadersWriterLock.h"
#include <list>
#include <map>
#include <ctime>
#include <memory>
#include <boost/scoped_ptr.hpp>
/**
* This is a Generic Cache implementation.
*
* To implement a cache using this class create a new class
* derived from CacheFactory and implement the lookup method.
* If the datasource supports updating implement update and
* remove method.
*
* EG
* typedef NameCache DynamicCache<int, BWString>;
* NameCacheFactory : NameCache::Factory
* {
* public:
* virtual bool lookup(int, BWString *);
* };
*
* NameCache cache(new NameCacheFactory, "<cache name>");
*
* --------------------------------------------------------
* Implementation note:
* This class uses a list as an efficient way to remove stale items from
* the cache. The map stores a key and an iterators to the data in the list.
* The list and the map are updated together.
*/
template <class KeyT, class DataT>
class CacheFactory
{
public:
virtual ~CacheFactory() {}
// Lookup the data for from the data source.
// Return true if the data is found.
virtual bool lookup(const KeyT & key, DataT * data) = 0;
// Update or insert the data in the data source.
// Return true if the data can be updated.
// Returning false means the cache is not updated either.
virtual bool update(const KeyT & key, const DataT & data) { return false; }
// Remove the data in the data source.
// Return true if the data can be deleted weather it exists or not.
// Returning false means the cache is not updated either.
virtual bool remove(const KeyT & key) { return false; }
};
template <class KeyT, class DataT>
class DynamicCache
{
public:
typedef CacheFactory<KeyT, DataT> Factory;
DynamicCache(Factory * f, const std::string & name, time_t t = (5 * 60)) :
factory(f), timeout(t), lastClean(std::time(0)), lock(name + " DynamicCache") {}
/*
* Lookup a key in the cache, the cached version is returned if it is
* present and the value is not old. If the value is old or is not
* present then use the factory to create it and insert the value in the
* cache for future lookups. If the factory cannot create it cache this
* fact too so we will ignore future lookups. Afterwards any entries in
* the cache longer than timeout are removed.
*
* This is the main method and entry point for the cache. All locking is
* performed inside the child methods.
*/
bool lookup(const KeyT & key, DataT * data, time_t now = std::time(0))
{
bool found = false;
FindStatus status = find(key, data, now);
switch(status & EntryStatus) {
case Found:
found = true;
break;
case Create:
found = build(key, data, now);
break;
}
if (status & CleanRequired) {
cleanOldEntries(now);
}
return found;
}
bool update(const KeyT & key, const DataT & data, time_t now = std::time(0))
{
if (factory->update(key, data))
{
Record record;
record.key = key;
record.createTime = now;
record.data = data;
record.exists = true;
BWReadersWriterLock::WriteLockGuard guard(lock, __FILE__, __LINE__);
updateEntry(key, record);
return true;
}
return false;
}
bool remove(const KeyT & key, time_t now = std::time(0))
{
if (factory->remove(key))
{
Record record;
record.key = key;
record.createTime = now;
record.exists = false;
BWReadersWriterLock::WriteLockGuard guard(lock, __FILE__, __LINE__);
updateEntry(key, record);
return true;
}
return false;
}
/**
* Return the size of the cache (only really useful for unit testing).
*/
size_t size() const
{
BWReadersWriterLock::ReadLockGuard guard(lock, __FILE__, __LINE__);
return map.size();
}
Factory * getFactory()
{
return factory.get();
}
private:
// Cache record
struct Record
{
KeyT key;
DataT data;
time_t createTime;
bool exists;
};
// Find and Clean status
// CleanRequired is part of this so that searching the cache and finding
// stale items in the cache can be automic (use a single readlock).
enum FindStatus {
None,
Found,
Create, //Add
NotExist,
EntryStatus=Found|Create|NotExist,
CleanRequired = 8
};
typedef std::list<Record> List;
typedef typename List::iterator Iterator;
typedef std::map<KeyT, typename Iterator> Map;
//
// The following methods all use and require explicit locking.
//
FindStatus find(const KeyT & key, DataT * data, time_t now)
{
BWReadersWriterLock::ReadLockGuard guard(lock, __FILE__, __LINE__);
Iterator itr = getEntry(key);
if (isValid(itr) && !isOld(itr, now)) {
if (itr->exists) {
*data = itr->data;
return FindStatus(Found | cleanRequired(now));
}
else {
return FindStatus(NotExist | cleanRequired(now));
}
}
return FindStatus(Create | cleanRequired(now));
}
bool build(const KeyT & key, DataT * data, time_t now)
{
Record record;
record.key = key;
record.createTime = now;
record.exists = factory->lookup(key, &record.data);
BWReadersWriterLock::WriteLockGuard guard(lock, __FILE__, __LINE__);
if (record.exists) {
*data = record.data;
}
updateEntry(key, record);
return record.exists;
}
void cleanOldEntries(time_t now)
{
BWReadersWriterLock::WriteLockGuard guard(lock, __FILE__, __LINE__);
lastClean = now;
time_t old = now - timeout;
typename List::reverse_iterator itr = list.rbegin();
while(!list.empty() && list.back().createTime < old) {
removeEntry(getEntry(list.back().key));
}
}
//
// The following methods don't use locking but require the calling
// method to already have aquired a lock.
//
Iterator getEntry(const KeyT & key)
{
typename Map::const_iterator itr = map.find(key);
if (itr != map.end()) {
return map.find(key)->second;
}
return list.end();
}
bool updateEntry(const KeyT key, const Record & record)
{
Iterator itr = getEntry(key);
if (isValid(itr)) {
removeEntry(itr);
}
insertEntry(record);
return record.exists;
}
bool isValid(Iterator itr) const
{
typename List::const_iterator constItr(itr);
return constItr != list.end();
}
bool isOld(Iterator itr, time_t now) const
{
// isOld or time_t has wrapped
return ((itr->createTime + timeout) < now) || (now < itr->createTime);
}
Iterator insertEntry(const Record & record)
{
list.push_front(record);
Iterator itr = list.begin();
map.insert(typename Map::value_type(record.key, itr));
return itr;
}
void removeEntry(Iterator itr)
{
map.erase(itr->key);
list.erase(itr);
}
FindStatus cleanRequired(time_t now) const
{
return (lastClean + timeout) < now ? CleanRequired : None;
}
List list;
Map map;
time_t timeout;
time_t lastClean;
boost::scoped_ptr<CacheFactory<KeyT, DataT> > factory;
mutable BWReadersWriterLock lock;
};
gracias esto parece exactamente lo que necesito! – Stowelly
Hay un problema aquí. Si accede a un ítem más de una vez, entonces el deque contendrá múltiples copias de su iterador, lo que resultará en tratar de borrar el mismo ítem dos veces (causando un comportamiento indefinido). Debe quitar el elemento en el acceso y volver a insertarlo en la parte delantera. Una lista sería más eficiente que una deque para esto. –
Una deque es lo suficientemente rápida para colecciones pequeñas (~ 100/~ 1000 elementos). Estamos hablando de iteradores (punteros) después de todo. –