Cache: shared_ptr used with CacheRules and StorageFactory

shared_ptr is now used for managing the lifetime of CacheRules
and StorageFactory instances.
This commit is contained in:
Johan Wikman
2016-11-30 09:47:12 +02:00
parent d52145054f
commit 93103fd64a
10 changed files with 88 additions and 73 deletions

View File

@ -21,19 +21,17 @@
Cache::Cache(const std::string& name, Cache::Cache(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory) SStorageFactory sFactory)
: m_name(name) : m_name(name)
, m_config(*pConfig) , m_config(*pConfig)
, m_pRules(pRules) , m_sRules(sRules)
, m_pFactory(pFactory) , m_sFactory(sFactory)
{ {
} }
Cache::~Cache() Cache::~Cache()
{ {
delete m_pRules;
delete m_pFactory;
} }
//static //static
@ -92,10 +90,10 @@ bool Cache::Create(const CACHE_CONFIG& config,
bool Cache::should_store(const char* zDefaultDb, const GWBUF* pQuery) bool Cache::should_store(const char* zDefaultDb, const GWBUF* pQuery)
{ {
return m_pRules->should_store(zDefaultDb, pQuery); return m_sRules->should_store(zDefaultDb, pQuery);
} }
bool Cache::should_use(const SESSION* pSession) bool Cache::should_use(const SESSION* pSession)
{ {
return m_pRules->should_use(pSession); return m_sRules->should_use(pSession);
} }

View File

@ -14,6 +14,7 @@
#include <maxscale/cdefs.h> #include <maxscale/cdefs.h>
#include <tr1/functional> #include <tr1/functional>
#include <tr1/memory>
#include <string> #include <string>
#include <maxscale/buffer.h> #include <maxscale/buffer.h>
#include <maxscale/session.h> #include <maxscale/session.h>
@ -25,6 +26,9 @@ class SessionCache;
class Cache class Cache
{ {
public: public:
typedef std::tr1::shared_ptr<CacheRules> SCacheRules;
typedef std::tr1::shared_ptr<StorageFactory> SStorageFactory;
virtual ~Cache(); virtual ~Cache();
const CACHE_CONFIG& config() const { return m_config; } const CACHE_CONFIG& config() const { return m_config; }
@ -77,8 +81,8 @@ public:
protected: protected:
Cache(const std::string& name, Cache(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory); SStorageFactory sFactory);
static bool Create(const CACHE_CONFIG& config, static bool Create(const CACHE_CONFIG& config,
CacheRules** ppRules); CacheRules** ppRules);
@ -94,6 +98,6 @@ private:
protected: protected:
const std::string m_name; // The name of the instance; the section name in the config. const std::string m_name; // The name of the instance; the section name in the config.
const CACHE_CONFIG& m_config; // The configuration of the cache instance. const CACHE_CONFIG& m_config; // The configuration of the cache instance.
CacheRules* m_pRules; // The rules of the cache instance. SCacheRules m_sRules; // The rules of the cache instance.
StorageFactory* m_pFactory; // The storage factory. SStorageFactory m_sFactory; // The storage factory.
}; };

View File

@ -16,12 +16,14 @@
#include "storage.h" #include "storage.h"
#include "storagefactory.h" #include "storagefactory.h"
using std::tr1::shared_ptr;
CacheMT::CacheMT(const std::string& name, CacheMT::CacheMT(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage) Storage* pStorage)
: CacheSimple(name, pConfig, pRules, pFactory, pStorage) : CacheSimple(name, pConfig, sRules, sFactory, pStorage)
{ {
spinlock_init(&m_lockPending); spinlock_init(&m_lockPending);
@ -43,17 +45,20 @@ CacheMT* CacheMT::Create(const std::string& name, const CACHE_CONFIG* pConfig)
if (CacheSimple::Create(*pConfig, &pRules, &pFactory)) if (CacheSimple::Create(*pConfig, &pRules, &pFactory))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
shared_ptr<StorageFactory> sFactory(pFactory);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
} }
// static // static
CacheMT* CacheMT::Create(const std::string& name, StorageFactory* pFactory, const CACHE_CONFIG* pConfig) CacheMT* CacheMT::Create(const std::string& name, SStorageFactory sFactory, const CACHE_CONFIG* pConfig)
{ {
ss_dassert(pConfig); ss_dassert(pConfig);
ss_dassert(pFactory); ss_dassert(sFactory.get());
CacheMT* pCache = NULL; CacheMT* pCache = NULL;
@ -61,7 +66,9 @@ CacheMT* CacheMT::Create(const std::string& name, StorageFactory* pFactory, cons
if (CacheSimple::Create(*pConfig, &pRules)) if (CacheSimple::Create(*pConfig, &pRules))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
@ -84,8 +91,8 @@ void CacheMT::refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache
// static // static
CacheMT* CacheMT::Create(const std::string& name, CacheMT* CacheMT::Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory) SStorageFactory sFactory)
{ {
CacheMT* pCache = NULL; CacheMT* pCache = NULL;
@ -96,7 +103,7 @@ CacheMT* CacheMT::Create(const std::string& name,
int argc = pConfig->storage_argc; int argc = pConfig->storage_argc;
char** argv = pConfig->storage_argv; char** argv = pConfig->storage_argv;
Storage* pStorage = pFactory->createStorage(CACHE_THREAD_MODEL_MT, name.c_str(), Storage* pStorage = sFactory->createStorage(CACHE_THREAD_MODEL_MT, name.c_str(),
ttl, maxCount, maxSize, ttl, maxCount, maxSize,
argc, argv); argc, argv);
@ -104,15 +111,13 @@ CacheMT* CacheMT::Create(const std::string& name,
{ {
CPP_GUARD(pCache = new CacheMT(name, CPP_GUARD(pCache = new CacheMT(name,
pConfig, pConfig,
pRules, sRules,
pFactory, sFactory,
pStorage)); pStorage));
if (!pCache) if (!pCache)
{ {
delete pStorage; delete pStorage;
delete pRules;
delete pFactory;
} }
} }

View File

@ -22,7 +22,7 @@ public:
~CacheMT(); ~CacheMT();
static CacheMT* Create(const std::string& name, const CACHE_CONFIG* pConfig); static CacheMT* Create(const std::string& name, const CACHE_CONFIG* pConfig);
static CacheMT* Create(const std::string& name, StorageFactory* pFactory, const CACHE_CONFIG* pConfig); static CacheMT* Create(const std::string& name, SStorageFactory sFactory, const CACHE_CONFIG* pConfig);
bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache); bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache);
@ -31,14 +31,14 @@ public:
private: private:
CacheMT(const std::string& name, CacheMT(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage); Storage* pStorage);
static CacheMT* Create(const std::string& name, static CacheMT* Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory); SStorageFactory sFactory);
private: private:
CacheMT(const CacheMT&); CacheMT(const CacheMT&);

View File

@ -47,10 +47,10 @@ inline int thread_index()
CachePT::CachePT(const std::string& name, CachePT::CachePT(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
const Caches& caches) const Caches& caches)
: Cache(name, pConfig, pRules, pFactory) : Cache(name, pConfig, sRules, sFactory)
, m_caches(caches) , m_caches(caches)
{ {
MXS_NOTICE("Created cache per thread."); MXS_NOTICE("Created cache per thread.");
@ -72,15 +72,18 @@ CachePT* CachePT::Create(const std::string& name, const CACHE_CONFIG* pConfig)
if (Cache::Create(*pConfig, &pRules, &pFactory)) if (Cache::Create(*pConfig, &pRules, &pFactory))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
shared_ptr<StorageFactory> sFactory(pFactory);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
} }
// static // static
CachePT* CachePT::Create(const std::string& name, CachePT* CachePT::Create(const std::string& name,
StorageFactory* pFactory, SStorageFactory sFactory,
const CACHE_CONFIG* pConfig) const CACHE_CONFIG* pConfig)
{ {
ss_dassert(pConfig); ss_dassert(pConfig);
@ -91,7 +94,9 @@ CachePT* CachePT::Create(const std::string& name,
if (Cache::Create(*pConfig, &pRules)) if (Cache::Create(*pConfig, &pRules))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
@ -130,8 +135,8 @@ cache_result_t CachePT::del_value(const CACHE_KEY& key)
// static // static
CachePT* CachePT::Create(const std::string& name, CachePT* CachePT::Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory) SStorageFactory sFactory)
{ {
CachePT* pCache = NULL; CachePT* pCache = NULL;
@ -153,7 +158,7 @@ CachePT* CachePT::Create(const std::string& name,
CacheST* pCacheST = 0; CacheST* pCacheST = 0;
CPP_GUARD(pCacheST = CacheST::Create(namest, pFactory, pConfig)); CPP_GUARD(pCacheST = CacheST::Create(namest, sFactory, pConfig));
if (pCacheST) if (pCacheST)
{ {
@ -171,13 +176,11 @@ CachePT* CachePT::Create(const std::string& name,
if (!error) if (!error)
{ {
pCache = new CachePT(name, pConfig, pRules, pFactory, caches); pCache = new CachePT(name, pConfig, sRules, sFactory, caches);
} }
} }
catch (const std::exception&) catch (const std::exception&)
{ {
delete pRules;
delete pFactory;
} }
return pCache; return pCache;

View File

@ -23,7 +23,7 @@ public:
~CachePT(); ~CachePT();
static CachePT* Create(const std::string& name, const CACHE_CONFIG* pConfig); static CachePT* Create(const std::string& name, const CACHE_CONFIG* pConfig);
static CachePT* Create(const std::string& name, StorageFactory* pFactory, const CACHE_CONFIG* pConfig); static CachePT* Create(const std::string& name, SStorageFactory sFactory, const CACHE_CONFIG* pConfig);
bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache); bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache);
@ -43,14 +43,14 @@ private:
CachePT(const std::string& name, CachePT(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
const Caches& caches); const Caches& caches);
static CachePT* Create(const std::string& name, static CachePT* Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory); SStorageFactory sFactory);
Cache& thread_cache(); Cache& thread_cache();

View File

@ -18,10 +18,10 @@
CacheSimple::CacheSimple(const std::string& name, CacheSimple::CacheSimple(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage) Storage* pStorage)
: Cache(name, pConfig, pRules, pFactory) : Cache(name, pConfig, sRules, sFactory)
, m_pStorage(pStorage) , m_pStorage(pStorage)
{ {
} }

View File

@ -35,8 +35,8 @@ public:
protected: protected:
CacheSimple(const std::string& name, CacheSimple(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage); Storage* pStorage);
static bool Create(const CACHE_CONFIG& config, static bool Create(const CACHE_CONFIG& config,

View File

@ -16,12 +16,14 @@
#include "storage.h" #include "storage.h"
#include "storagefactory.h" #include "storagefactory.h"
using std::tr1::shared_ptr;
CacheST::CacheST(const std::string& name, CacheST::CacheST(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage) Storage* pStorage)
: CacheSimple(name, pConfig, pRules, pFactory, pStorage) : CacheSimple(name, pConfig, sRules, sFactory, pStorage)
{ {
MXS_NOTICE("Created single threaded cache."); MXS_NOTICE("Created single threaded cache.");
} }
@ -41,17 +43,20 @@ CacheST* CacheST::Create(const std::string& name, const CACHE_CONFIG* pConfig)
if (CacheSimple::Create(*pConfig, &pRules, &pFactory)) if (CacheSimple::Create(*pConfig, &pRules, &pFactory))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
shared_ptr<StorageFactory> sFactory(pFactory);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
} }
// static // static
CacheST* CacheST::Create(const std::string& name, StorageFactory* pFactory, const CACHE_CONFIG* pConfig) CacheST* CacheST::Create(const std::string& name, SStorageFactory sFactory, const CACHE_CONFIG* pConfig)
{ {
ss_dassert(pConfig); ss_dassert(pConfig);
ss_dassert(pFactory); ss_dassert(sFactory.get());
CacheST* pCache = NULL; CacheST* pCache = NULL;
@ -59,7 +64,9 @@ CacheST* CacheST::Create(const std::string& name, StorageFactory* pFactory, cons
if (CacheSimple::Create(*pConfig, &pRules)) if (CacheSimple::Create(*pConfig, &pRules))
{ {
pCache = Create(name, pConfig, pRules, pFactory); shared_ptr<CacheRules> sRules(pRules);
pCache = Create(name, pConfig, sRules, sFactory);
} }
return pCache; return pCache;
@ -78,8 +85,8 @@ void CacheST::refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache
// static // static
CacheST* CacheST::Create(const std::string& name, CacheST* CacheST::Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory) SStorageFactory sFactory)
{ {
CacheST* pCache = NULL; CacheST* pCache = NULL;
@ -90,7 +97,7 @@ CacheST* CacheST::Create(const std::string& name,
int argc = pConfig->storage_argc; int argc = pConfig->storage_argc;
char** argv = pConfig->storage_argv; char** argv = pConfig->storage_argv;
Storage* pStorage = pFactory->createStorage(CACHE_THREAD_MODEL_ST, name.c_str(), Storage* pStorage = sFactory->createStorage(CACHE_THREAD_MODEL_ST, name.c_str(),
ttl, maxCount, maxSize, ttl, maxCount, maxSize,
argc, argv); argc, argv);
@ -98,15 +105,13 @@ CacheST* CacheST::Create(const std::string& name,
{ {
CPP_GUARD(pCache = new CacheST(name, CPP_GUARD(pCache = new CacheST(name,
pConfig, pConfig,
pRules, sRules,
pFactory, sFactory,
pStorage)); pStorage));
if (!pCache) if (!pCache)
{ {
delete pStorage; delete pStorage;
delete pRules;
delete pFactory;
} }
} }

View File

@ -21,7 +21,7 @@ public:
~CacheST(); ~CacheST();
static CacheST* Create(const std::string& name, const CACHE_CONFIG* pConfig); static CacheST* Create(const std::string& name, const CACHE_CONFIG* pConfig);
static CacheST* Create(const std::string& name, StorageFactory* pFactory, const CACHE_CONFIG* pConfig); static CacheST* Create(const std::string& name, SStorageFactory sFactory, const CACHE_CONFIG* pConfig);
bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache); bool must_refresh(const CACHE_KEY& key, const SessionCache* pSessionCache);
@ -30,14 +30,14 @@ public:
private: private:
CacheST(const std::string& name, CacheST(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory, SStorageFactory sFactory,
Storage* pStorage); Storage* pStorage);
static CacheST* Create(const std::string& name, static CacheST* Create(const std::string& name,
const CACHE_CONFIG* pConfig, const CACHE_CONFIG* pConfig,
CacheRules* pRules, SCacheRules sRules,
StorageFactory* pFactory); SStorageFactory sFactory);
private: private:
CacheST(const CacheST&); CacheST(const CacheST&);
CacheST& operator = (const CacheST&); CacheST& operator = (const CacheST&);