Replaced local file caching by persistent ObjectCache based on FileDb.

Removed IsCached and ImageType properties from TileLayer.
This commit is contained in:
ClemensF 2012-07-03 18:03:56 +02:00
parent 38e6c23114
commit 9652fc2f56
13 changed files with 2297 additions and 148 deletions

File diff suppressed because it is too large Load diff

Binary file not shown.

View file

@ -0,0 +1,5 @@
FileDbCache uses FileDb by Brett Goodman (aka EzTools), a simple No-SQL database for .NET.
FileDb is Open Source on Google Code Hosting at http://code.google.com/p/filedb-database/
and licensed under the Apache License 2.0, http://www.apache.org/licenses/LICENSE-2.0.
See the product homepage at http://www.eztools-software.com/tools/filedb/.
Download FileDb from http://www.eztools-software.com/downloads/filedb.exe.

View file

@ -0,0 +1,444 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Caching;
using System.Runtime.Serialization.Formatters.Binary;
using FileDbNs;
namespace Caching
{
/// <summary>
/// ObjectCache implementation based on EzTools FileDb - http://www.eztools-software.com/tools/filedb/.
/// </summary>
public class FileDbCache : ObjectCache, IDisposable
{
private const string keyField = "Key";
private const string valueField = "Value";
private const string expiresField = "Expires";
private readonly BinaryFormatter formatter = new BinaryFormatter();
private readonly FileDb fileDb = new FileDb();
private readonly string name;
private readonly string path;
public FileDbCache(string name, string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("The parameter path must not be null or empty.");
}
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path += ".fdb";
}
this.name = name;
this.path = path;
try
{
fileDb.Open(path, false);
}
catch
{
CreateDatebase();
}
Trace.TraceInformation("FileDbCache created with {0} cached items", fileDb.NumRecords);
}
public bool AutoFlush
{
get { return fileDb.AutoFlush; }
set { fileDb.AutoFlush = value; }
}
public int AutoCleanThreshold
{
get { return fileDb.AutoCleanThreshold; }
set { fileDb.AutoCleanThreshold = value; }
}
public override string Name
{
get { return name; }
}
public override DefaultCacheCapabilities DefaultCacheCapabilities
{
get { return DefaultCacheCapabilities.InMemoryProvider | DefaultCacheCapabilities.AbsoluteExpirations | DefaultCacheCapabilities.SlidingExpirations; }
}
public override object this[string key]
{
get { return Get(key); }
set { Set(key, value, null); }
}
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
throw new NotSupportedException("FileDbCache does not support the ability to enumerate items.");
}
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
{
throw new NotSupportedException("FileDbCache does not support the ability to create change monitors.");
}
public override long GetCount(string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
long count = 0;
try
{
count = fileDb.NumRecords;
}
catch
{
if (CheckReindex())
{
try
{
count = fileDb.NumRecords;
}
catch
{
CreateDatebase();
}
}
}
return count;
}
public override bool Contains(string key, string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
bool contains = false;
try
{
contains = fileDb.GetRecordByKey(key, new string[0], false) != null;
}
catch
{
if (CheckReindex())
{
try
{
contains = fileDb.GetRecordByKey(key, new string[0], false) != null;
}
catch
{
CreateDatebase();
}
}
}
return contains;
}
public override object Get(string key, string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
object value = null;
Record record = null;
try
{
record = fileDb.GetRecordByKey(key, new string[] { valueField }, false);
}
catch
{
if (CheckReindex())
{
try
{
record = fileDb.GetRecordByKey(key, new string[] { valueField }, false);
}
catch
{
CreateDatebase();
}
}
}
if (record != null)
{
try
{
using (MemoryStream stream = new MemoryStream((byte[])record[0]))
{
value = formatter.Deserialize(stream);
}
}
catch (Exception exc)
{
Trace.TraceWarning("FileDbCache.Get({0}): {1}", key, exc.Message);
try
{
fileDb.DeleteRecordByKey(key);
}
catch
{
}
}
}
return value;
}
public override CacheItem GetCacheItem(string key, string regionName = null)
{
var value = Get(key, regionName);
return value != null ? new CacheItem(key, value) : null;
}
public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
var values = new Dictionary<string, object>();
foreach (string key in keys)
{
values[key] = Get(key);
}
return values;
}
public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
if (value == null)
{
throw new ArgumentNullException("The parameter value must not be null.");
}
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
byte[] valueBuffer = null;
try
{
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, value);
valueBuffer = stream.ToArray();
}
}
catch (Exception exc)
{
Trace.TraceWarning("FileDbCache.Set({0}): {1}", key, exc.Message);
}
if (valueBuffer != null)
{
DateTime expires = DateTime.MaxValue;
if (policy.AbsoluteExpiration != InfiniteAbsoluteExpiration)
{
expires = policy.AbsoluteExpiration.DateTime;
}
else if (policy.SlidingExpiration != NoSlidingExpiration)
{
expires = DateTime.UtcNow + policy.SlidingExpiration;
}
try
{
AddOrUpdateRecord(key, valueBuffer, expires);
}
catch
{
if (CheckReindex())
{
try
{
AddOrUpdateRecord(key, valueBuffer, expires);
}
catch
{
CreateDatebase();
AddOrUpdateRecord(key, valueBuffer, expires);
}
}
}
}
}
public override void Set(CacheItem item, CacheItemPolicy policy)
{
Set(item.Key, item.Value, policy, item.RegionName);
}
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
}
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
{
var oldValue = Get(key, regionName);
Set(key, value, policy);
return oldValue;
}
public override CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
{
var oldItem = GetCacheItem(item.Key, item.RegionName);
Set(item, policy);
return oldItem;
}
public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
return AddOrGetExisting(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
}
public override object Remove(string key, string regionName = null)
{
var oldValue = Get(key, regionName);
if (oldValue != null)
{
try
{
fileDb.DeleteRecordByKey(key);
}
catch
{
}
}
return oldValue;
}
public void Flush()
{
try
{
fileDb.Flush();
}
catch
{
CheckReindex();
}
}
public void Clean()
{
try
{
fileDb.Clean();
}
catch
{
CheckReindex();
}
}
public void Dispose()
{
try
{
fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, EqualityEnum.LessThanOrEqual));
Trace.TraceInformation("FileDbCache has deleted {0} expired items", fileDb.NumDeleted);
fileDb.Clean();
fileDb.Close();
}
catch
{
if (CheckReindex())
{
fileDb.Close();
}
}
}
private bool CheckReindex()
{
if (fileDb.IsOpen)
{
Trace.TraceWarning("FileDbCache is reindexing database");
fileDb.Reindex();
return true;
}
return false;
}
private void CreateDatebase()
{
if (File.Exists(path))
{
File.Delete(path);
}
else
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
fileDb.Create(path, new Field[]
{
new Field(keyField, DataTypeEnum.String) { IsPrimaryKey = true },
new Field(valueField, DataTypeEnum.Byte) { IsArray = true },
new Field(expiresField, DataTypeEnum.DateTime)
});
}
private void AddOrUpdateRecord(string key, object value, DateTime expires)
{
var fieldValues = new FieldValues(3); // capacity
fieldValues.Add(valueField, value);
fieldValues.Add(expiresField, expires);
if (fileDb.GetRecordByKey(key, new string[0], false) == null)
{
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
}
else
{
fileDb.UpdateRecordByKey(key, fieldValues);
}
}
}
}

View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{EF44F661-B98A-4676-927F-85D138F82300}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Caching</RootNamespace>
<AssemblyName>FileDbCache</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FileDb, Version=3.10.0.0, Culture=neutral, PublicKeyToken=68cc942b9efb3282, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>FileDb\FileDb.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileDbCache.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="FileDb\FileDb.dll" />
<Content Include="FileDb\FileDb.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FileDbCache")]
[assembly: AssemblyDescription("ObjectCache implementation based on EzTools FileDb")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FileDbCache")]
[assembly: AssemblyCopyright("Copyright © 2012 Clemens Fischer")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c1fc4f52-e47c-4f62-9807-7e096db69851")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]