// Copyright (c) 2010 Martin Knafve / hMailServer.com. // http://www.hmailserver.com #include "StdAfx.h" #include "CacheContainer.h" #include "CacheConfiguration.h" #include "Cache.h" #include "../Application/PropertySet.h" #include "../Application/Property.h" #include "../BO/Domain.h" #include "../BO/Account.h" #include "../BO/Alias.h" #include "../BO/DistributionList.h" #include "../BO/Group.h" #include "../Persistence/PersistentDomain.h" #include "../Persistence/PersistentAccount.h" #include "../Persistence/PersistentAlias.h" #include "../Persistence/PersistentDistributionList.h" #include "../Persistence/PersistentGroup.h" #include "../Persistence/PersistentServerMessage.h" #include "CacheReaderWithDbFallback.h" #ifdef _DEBUG #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif namespace HM { CacheContainer::CacheContainer(void) { // Limit the cache of each entity to 10MB each. Cache::Instance()->SetMaxSize(10 * 1024 * 1024); Cache::Instance()->SetMaxSize(10 * 1024 * 1024); Cache::Instance()->SetMaxSize(10 * 1024 * 1024); Cache::Instance()->SetMaxSize(10 * 1024 * 1024); // When the server is stopped, the cache should be cleared. Application::Instance()->OnServerStopped.connect ( [this]() { Cache::Instance()->Clear(); Cache::Instance()->Clear(); Cache::Instance()->Clear(); Cache::Instance()->Clear(); } ); } CacheContainer::~CacheContainer(void) { } std::shared_ptr CacheContainer::GetAccount(__int64 iID) { CacheReaderWithDbFallback reader; return reader.GetObject(iID); } std::shared_ptr CacheContainer::GetAccount(const String &sName) { CacheReaderWithDbFallback reader; return reader.GetObject(sName); } size_t CacheContainer::GetAccountCacheSize() { return Cache::Instance()->GetSize(); } void CacheContainer::SetAccountCacheMaxSize(size_t max_size) { return Cache::Instance()->SetMaxSize(max_size); } size_t CacheContainer::GetAccountCacheMaxSize() { return Cache::Instance()->GetMaxSize(); } std::shared_ptr CacheContainer::GetDomain(const String &sName) { #ifdef _DEBUG if (sName.Contains(_T("@"))) { assert(0); } #endif CacheReaderWithDbFallback reader; auto domain = reader.GetObject(sName); return domain; } std::shared_ptr CacheContainer::GetDomain(__int64 iID) { CacheReaderWithDbFallback reader; return reader.GetObject(iID); } size_t CacheContainer::GetDomainCacheSize() { return Cache::Instance()->GetSize(); } void CacheContainer::SetDomainCacheMaxSize(size_t max_size) { return Cache::Instance()->SetMaxSize(max_size); } size_t CacheContainer::GetDomainCacheMaxSize() { return Cache::Instance()->GetMaxSize(); } void CacheContainer::RemoveDomain(std::shared_ptr domain) { return Cache::Instance()->RemoveObject(domain); } std::shared_ptr CacheContainer::GetAlias(const String &sName) { CacheReaderWithDbFallback reader; return reader.GetObject(sName); } std::shared_ptr CacheContainer::GetAlias(__int64 iID) { CacheReaderWithDbFallback reader; return reader.GetObject(iID); } size_t CacheContainer::GetAliasCacheSize() { return Cache::Instance()->GetSize(); } void CacheContainer::SetAliasCacheMaxSize(size_t max_size) { return Cache::Instance()->SetMaxSize(max_size); } size_t CacheContainer::GetAliasCacheMaxSize() { return Cache::Instance()->GetMaxSize(); } std::shared_ptr CacheContainer::GetDistributionList(const String &sName) { CacheReaderWithDbFallback reader; return reader.GetObject(sName); } std::shared_ptr CacheContainer::GetDistributionList(__int64 iID) { CacheReaderWithDbFallback reader; return reader.GetObject(iID); } size_t CacheContainer::GetDistributionListCacheSize() { return Cache::Instance()->GetSize(); } void CacheContainer::SetDistributionListCacheMaxSize(size_t max_size) { return Cache::Instance()->SetMaxSize (max_size); } size_t CacheContainer::GetDistributionListCacheMaxSize() { return Cache::Instance()->GetMaxSize(); } void CacheContainer::OnPropertyChanged(std::shared_ptr pProperty) { String sPropName = pProperty->GetName(); if (sPropName == PROPERTY_USECACHE) { bool bMainCacheEnabled = pProperty->GetBoolValue(); Cache::Instance()->SetEnabled(bMainCacheEnabled); Cache::Instance()->SetEnabled(bMainCacheEnabled); Cache::Instance()->SetEnabled(bMainCacheEnabled); Cache::Instance()->SetEnabled(bMainCacheEnabled); } else if (sPropName == PROPERTY_DOMAINCACHETTL) Cache::Instance()->SetTTL(pProperty->GetLongValue()); else if (sPropName == PROPERTY_ACCOUNTCACHETTL) Cache::Instance()->SetTTL(pProperty->GetLongValue()); else if (sPropName == PROPERTY_ALIASCACHETTL) Cache::Instance()->SetTTL(pProperty->GetLongValue()); else if (sPropName == PROPERTY_DISTRIBUTIONLISTCACHETTL) Cache::Instance()->SetTTL(pProperty->GetLongValue()); } InboxIDCache & CacheContainer::GetInboxIDCache() { return inbox_idcache_; } void CacheContainer::Clear() { inbox_idcache_.Clear(); } }