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,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory)
SCacheRules sRules,
SStorageFactory sFactory)
: m_name(name)
, m_config(*pConfig)
, m_pRules(pRules)
, m_pFactory(pFactory)
, m_sRules(sRules)
, m_sFactory(sFactory)
{
}
Cache::~Cache()
{
delete m_pRules;
delete m_pFactory;
}
//static
@ -92,10 +90,10 @@ bool Cache::Create(const CACHE_CONFIG& config,
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)
{
return m_pRules->should_use(pSession);
return m_sRules->should_use(pSession);
}

View File

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

View File

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

View File

@ -22,7 +22,7 @@ public:
~CacheMT();
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);
@ -31,14 +31,14 @@ public:
private:
CacheMT(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory,
SCacheRules sRules,
SStorageFactory sFactory,
Storage* pStorage);
static CacheMT* Create(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory);
SCacheRules sRules,
SStorageFactory sFactory);
private:
CacheMT(const CacheMT&);

View File

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

View File

@ -23,7 +23,7 @@ public:
~CachePT();
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);
@ -43,14 +43,14 @@ private:
CachePT(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory,
SCacheRules sRules,
SStorageFactory sFactory,
const Caches& caches);
static CachePT* Create(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory);
SCacheRules sRules,
SStorageFactory sFactory);
Cache& thread_cache();

View File

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

View File

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

View File

@ -16,12 +16,14 @@
#include "storage.h"
#include "storagefactory.h"
using std::tr1::shared_ptr;
CacheST::CacheST(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory,
SCacheRules sRules,
SStorageFactory sFactory,
Storage* pStorage)
: CacheSimple(name, pConfig, pRules, pFactory, pStorage)
: CacheSimple(name, pConfig, sRules, sFactory, pStorage)
{
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))
{
pCache = Create(name, pConfig, pRules, pFactory);
shared_ptr<CacheRules> sRules(pRules);
shared_ptr<StorageFactory> sFactory(pFactory);
pCache = Create(name, pConfig, sRules, sFactory);
}
return pCache;
}
// 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(pFactory);
ss_dassert(sFactory.get());
CacheST* pCache = NULL;
@ -59,7 +64,9 @@ CacheST* CacheST::Create(const std::string& name, StorageFactory* pFactory, cons
if (CacheSimple::Create(*pConfig, &pRules))
{
pCache = Create(name, pConfig, pRules, pFactory);
shared_ptr<CacheRules> sRules(pRules);
pCache = Create(name, pConfig, sRules, sFactory);
}
return pCache;
@ -78,8 +85,8 @@ void CacheST::refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache
// static
CacheST* CacheST::Create(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory)
SCacheRules sRules,
SStorageFactory sFactory)
{
CacheST* pCache = NULL;
@ -90,7 +97,7 @@ CacheST* CacheST::Create(const std::string& name,
int argc = pConfig->storage_argc;
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,
argc, argv);
@ -98,15 +105,13 @@ CacheST* CacheST::Create(const std::string& name,
{
CPP_GUARD(pCache = new CacheST(name,
pConfig,
pRules,
pFactory,
sRules,
sFactory,
pStorage));
if (!pCache)
{
delete pStorage;
delete pRules;
delete pFactory;
}
}

View File

@ -21,7 +21,7 @@ public:
~CacheST();
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);
@ -30,14 +30,14 @@ public:
private:
CacheST(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory,
SCacheRules sRules,
SStorageFactory sFactory,
Storage* pStorage);
static CacheST* Create(const std::string& name,
const CACHE_CONFIG* pConfig,
CacheRules* pRules,
StorageFactory* pFactory);
SCacheRules sRules,
SStorageFactory sFactory);
private:
CacheST(const CacheST&);
CacheST& operator = (const CacheST&);