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)
{
m_AssetService = scene.RequestModuleInterface<IAssetService>();
}
}
}
@ -250,19 +249,11 @@ namespace Flotsam.RegionModules.AssetCache
private void UpdateMemoryCache(string key, AssetBase asset)
{
if (m_MemoryCacheEnabled)
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);
try
@ -271,8 +262,9 @@ namespace Flotsam.RegionModules.AssetCache
if (File.Exists(filename))
{
File.SetLastAccessTime(filename, DateTime.Now);
} else {
}
else
{
// 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
// same file multiple times.
@ -298,7 +290,6 @@ namespace Flotsam.RegionModules.AssetCache
m_CurrentlyWriting.Add(filename);
}
#endif
}
Util.FireAndForget(
@ -310,20 +301,45 @@ namespace Flotsam.RegionModules.AssetCache
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;
if (m_MemoryCacheEnabled && m_MemoryCache.TryGetValue(id, out asset))
{
if (m_MemoryCache.TryGetValue(id, out asset))
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);
if (File.Exists(filename))
{
@ -383,8 +399,21 @@ namespace Flotsam.RegionModules.AssetCache
m_RequestsForInprogress++;
}
#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))
{
m_HitRateFile = (double)m_DiskHits / m_Requests * 100.0;
@ -474,9 +503,9 @@ namespace Flotsam.RegionModules.AssetCache
/// removes empty tier directories.
/// </summary>
/// <param name="dir"></param>
/// <param name="purgeLine"></param>
private void CleanExpiredFiles(string dir, DateTime purgeLine)
{
foreach (string file in Directory.GetFiles(dir))
{
if (File.GetLastAccessTime(file) < purgeLine)