Show CacheManager.h syntax highlighted
#ifndef CACHEMANAGER_H
#define CACHEMANAGER_H
#include <string>
#include <time.h>
#include <IMAPFolder.h>
#include <IMAPTypes.h>
#include <Runnable.h>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
namespace Middleware
{
/**
* This class is responsible for keeping track of the various
* IMAP::IMAPFolder objects that have been created over time. The folder
* objects themselves are responsible for keeping track of the IMAP
* connection, and re-establishing it if it is lost.
*
* This class maintains an idea of how much memory is being used by all of
* the cached IMAP folder objects. When any of these limits are exceeded,
* it attempts to clean up the oldest items.
*
* It is sometimes necessary to use the IMAP connection directly (i.e. to
* create a folder, subscribe, etc. In those cases, obtain the IMAPFolder
* object for INBOX (since it is the thing always cached), and get the
* connection from it.
*
*/
class CacheManager : public Runnable
{
public:
CacheManager(bool *sflag) { shutdown_flag = sflag; }
~CacheManager() {}
/**
* Get a cache entry for an IMAPFolder object from the cache, creating
* one if necessary.
*
* @returns CacheEntry<IMAP::IMAPFolder> object connected to the IMAP
* server. The CacheEntry object insures that the memory for the imap
* folder is not cleaned by the cache sweeper while it is in use, so
* be sure to always access the folder through the entry (don't
* extract it and pass the pointer around!). The CacheEntry itself is
* reference counted, and it deletes the IMAPFolder when no one is
* using it anymore (i.e. the last CacheEntry for that folder is
* deleted).
*
* @throws IMAP::IMAPError If there is a problem connecting.
* @throws IMAP::BadFolder If folder is not valid.
*/
IMAP::CacheEntry<IMAP::IMAPFolder> getFolder(const std::string &host,
const std::string &user,
const std::string &password,
const std::string &folder);
/**
* Remove an IMAP folder from this cache. This deletes the CacheEntry,
* but the folder itself does not go away until all other cache
* entries which reference the folder are destroyed.
*/
void removeFromCache(const std::string &host,
const std::string &user,
const std::string &folder);
/**
* Flush all folder associated with user/host. This is useful for real
* logouts from the webmail system, so the cache does not have to time
* it out.
*/
void flush(const std::string &host, const std::string &user);
/**
* This is the code for a thread of operation for cache management
* tasks. It implements the actual cleanup routines, and should be
* started sometime during application setup.
*/
void operator()();
/**
* Set the maximum amount of memory cached. Note that this tries to
* account for overhead as well, but in general analyzes the number of
* bytes of data that are actually in the cache.
*/
void setMemoryLimit(u_int32_t sz) { memlimit = sz; }
/**
* Set the minimum amount of time that something will be cached. This
* is used by both the cleanup routine (when clearing IMAPFolder
* objects), and by IMAPFolder itself when flushing old
* MessageAttribute data.
*
* If set too high, then Folders might get flushed instead of
* attributes. If set too low, then the cache manager may have trouble
* keeping usage under the set memory limit.
*/
void setMinimumCacheTime(u_int32_t tm) { mincache = tm; }
/**
* Set the maximum amount of time that something will be cached if it
* has not been used. This is used by the cleanup routine when
* clearing IMAPFolder objects.
*
* This is essentially a way to force idle folders to be cleaned from
* the cache.
*/
void setMaximumCacheTime(u_int32_t tm) { maxcache = tm; }
u_int32_t getMemoryLimit() const { return memlimit; }
u_int32_t getMinimumCacheTime() const { return mincache; }
u_int32_t getMaximumCacheTime() const { return maxcache; }
private:
u_int32_t memlimit, mincache, maxcache;
/**
* Create a consistent key for a specific imap folder that is cached.
* This is used as the key to the CacheMap (cache) below.
*/
std::string makeKey(const std::string &user, const std::string &host,
const std::string &folder);
/**
* The actual map of folder keys to cache entries of IMAPFolder
* objects
*/
CacheMap cache;
boost::recursive_mutex cache_mutex;
/**
* This is a pointer to some sort of global shutdown flag. If this
* flag becomes true, then the CacheManager will terminate its thread
* within 30 seconds.
*/
bool *shutdown_flag;
};
extern CacheManager *theCacheManager;
};
#endif
See more files for this project here