refactor: Split file cache manipulation code into separate methods, as has already been done for the memory cache

bulletsim
Justin Clark-Casey (justincc) 2011-07-04 22:30:18 +01:00
parent 46f5893d55
commit 5dc785bbf2
1 changed files with 135 additions and 106 deletions

View File

@ -226,7 +226,6 @@ namespace Flotsam.RegionModules.AssetCache
if (m_AssetService == null) if (m_AssetService == null)
{ {
m_AssetService = scene.RequestModuleInterface<IAssetService>(); m_AssetService = scene.RequestModuleInterface<IAssetService>();
} }
} }
} }
@ -250,19 +249,11 @@ namespace Flotsam.RegionModules.AssetCache
private void UpdateMemoryCache(string key, AssetBase asset) private void UpdateMemoryCache(string key, AssetBase asset)
{ {
if (m_MemoryCacheEnabled)
m_MemoryCache.AddOrUpdate(key, asset, m_MemoryExpiration); m_MemoryCache.AddOrUpdate(key, asset, m_MemoryExpiration);
} }
public void Cache(AssetBase asset) private void UpdateFileCache(string key, AssetBase asset)
{ {
// TODO: Spawn this off to some seperate thread to do the actual writing
if (asset != null)
{
m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Caching asset with id {0}", asset.ID);
UpdateMemoryCache(asset.ID, asset);
string filename = GetFileName(asset.ID); string filename = GetFileName(asset.ID);
try try
@ -271,8 +262,9 @@ namespace Flotsam.RegionModules.AssetCache
if (File.Exists(filename)) if (File.Exists(filename))
{ {
File.SetLastAccessTime(filename, DateTime.Now); File.SetLastAccessTime(filename, DateTime.Now);
} else { }
else
{
// Once we start writing, make sure we flag that we're writing // Once we start writing, make sure we flag that we're writing
// that object to the cache so that we don't try to write the // that object to the cache so that we don't try to write the
// same file multiple times. // same file multiple times.
@ -298,7 +290,6 @@ namespace Flotsam.RegionModules.AssetCache
m_CurrentlyWriting.Add(filename); m_CurrentlyWriting.Add(filename);
} }
#endif #endif
} }
Util.FireAndForget( Util.FireAndForget(
@ -310,20 +301,45 @@ namespace Flotsam.RegionModules.AssetCache
LogException(e); LogException(e);
} }
} }
public void Cache(AssetBase asset)
{
// TODO: Spawn this off to some seperate thread to do the actual writing
if (asset != null)
{
//m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Caching asset with id {0}", asset.ID);
if (m_MemoryCacheEnabled)
UpdateMemoryCache(asset.ID, asset);
UpdateFileCache(asset.ID, asset);
}
} }
public AssetBase Get(string id) /// <summary>
/// Try to get an asset from the in-memory cache.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private AssetBase GetFromMemoryCache(string id)
{ {
m_Requests++;
AssetBase asset = null; AssetBase asset = null;
if (m_MemoryCacheEnabled && m_MemoryCache.TryGetValue(id, out asset)) if (m_MemoryCache.TryGetValue(id, out asset))
{
m_MemoryHits++; m_MemoryHits++;
return asset;
} }
else
/// <summary>
/// Try to get an asset from the file cache.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private AssetBase GetFromFileCache(string id)
{ {
AssetBase asset = null;
string filename = GetFileName(id); string filename = GetFileName(id);
if (File.Exists(filename)) if (File.Exists(filename))
{ {
@ -383,8 +399,21 @@ namespace Flotsam.RegionModules.AssetCache
m_RequestsForInprogress++; m_RequestsForInprogress++;
} }
#endif #endif
return asset;
} }
public AssetBase Get(string id)
{
m_Requests++;
AssetBase asset = null;
if (m_MemoryCacheEnabled)
asset = GetFromMemoryCache(id);
else
asset = GetFromFileCache(id);
if (((m_LogLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0)) if (((m_LogLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0))
{ {
m_HitRateFile = (double)m_DiskHits / m_Requests * 100.0; m_HitRateFile = (double)m_DiskHits / m_Requests * 100.0;
@ -474,9 +503,9 @@ namespace Flotsam.RegionModules.AssetCache
/// removes empty tier directories. /// removes empty tier directories.
/// </summary> /// </summary>
/// <param name="dir"></param> /// <param name="dir"></param>
/// <param name="purgeLine"></param>
private void CleanExpiredFiles(string dir, DateTime purgeLine) private void CleanExpiredFiles(string dir, DateTime purgeLine)
{ {
foreach (string file in Directory.GetFiles(dir)) foreach (string file in Directory.GetFiles(dir))
{ {
if (File.GetLastAccessTime(file) < purgeLine) if (File.GetLastAccessTime(file) < purgeLine)