From: Kevin W. <kev...@us...> - 2005-04-04 04:26:33
|
Update of /cvsroot/nhibernate/NHibernateContrib/src/NHibernate.Caches/Prevalence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28350 Added Files: prevalencecache.build prevalencecache.config PrevalenceCache.cs prevalencecache.nunit PrevalenceCacheFixture.cs PrevalenceCacheProvider.cs PrevalenceCacheProviderFixture.cs Log Message: migrating cache implementations to new folder --- NEW FILE: prevalencecache.build --- <?xml version="1.0"?> <project name="NHibernate.Caches.Prevalence" default="build" description="NHibernate pluggable cache provider using Bamboo.Prevalence engine" xmlns="http://nant.sourceforge.net/schemas/nant-0.84.win32.net-1.0.xsd"> <!-- Required properties: * build.dir - (path) root level to build to, assemblies will go in ${build.dir}/bin * build.debug - (true|false) debug build? * current.build.defines - framework-specific build defines * project.version - full project version * project.version.major - the major number of the build * project.version.minor - the minor number of the build * project.version.build - the build number * sign - (true|false)indicates if the Assembly should be signed. * clover.enabled - (true|false) indicates if Clover.NET should handle the build * clover.src - location of the clovered source to be stored at from the root of NHibernateContrib * clover.db - location of the coverage db from the root of NHibernateContrib --> <if propertytrue="clover.enabled"> <loadtasks assembly="${clover.home}/CloverNAnt-0.84.dll" /> </if> <property name="keyFile" value="..\NHibernate.snk" /> <target name="build"> <if propertytrue="clover.enabled"> <clover-setup initstring="..\..\${clover.db}" builddir="..\..\${clover.src}\${nant.project.name}" enabled="${clover.enabled}" flushinterval="1000" /> </if> <attrib file="AssemblyInfo.cs" readonly="false" /> <asminfo output="AssemblyInfo.cs" language="CSharp"> <imports> <import name="System" /> <import name="System.Reflection" /> <import name="System.Runtime.CompilerServices" /> <import name="System.Runtime.InteropServices" /> <import name="System.Security.Permissions" /> </imports> <attributes> <attribute type="AssemblyTitleAttribute" value="${nant.project.name} for ${current.runtime.description}" /> <attribute type="AssemblyDescriptionAttribute" value="Cache provider for NHibernate using Bamboo.Prevalence engine." /> <attribute type="AssemblyCompanyAttribute" value="nhibernate.sourceforge.net" /> <attribute type="AssemblyProductAttribute" value="${nant.project.name}" /> <attribute type="AssemblyCopyrightAttribute" value="Licensed under LGPL." /> <attribute type="AssemblyVersionAttribute" value="${project.version}" /> <attribute type="AssemblyInformationalVersionAttribute" value="${project.version.major}.${project.version.minor}" /> <attribute type="AssemblyKeyFileAttribute" value="${keyFile}" if="${sign}" /> </attributes> </asminfo> <csc output="${build.dir}/bin/${nant.project.name}.dll" doc="${build.dir}/bin/${nant.project.name}.xml" target="library" debug="${build.debug}" define="${current.build.defines}" optimize="true" > <sources> <excludes name="**/*Fixture.cs" /> <includes name="**/*.cs" /> </sources> <references> <includes name="System.dll" /> <includes name="System.Data.dll" /> <includes name="System.Web.dll" /> <includes name="System.XML.dll" /> <includes name="${build.dir}/bin/NHibernate.dll" /> <includes name="${build.dir}/bin/log4net.dll" /> <includes name="${build.dir}/bin/Bamboo.Prevalence.dll" /> </references> </csc> <csc output="${build.dir}/bin/${nant.project.name}.Tests.dll" target="library" debug="${build.debug}" define="${current.build.defines}" optimize="true" > <sources> <includes name="**/AssemblyInfo.cs" /> <includes name="**/*Fixture.cs" /> </sources> <references> <includes name="System.dll" /> <includes name="System.Data.dll" /> <includes name="System.XML.dll" /> <includes name="${build.dir}/bin/${nant.project.name}.dll" /> <includes name="${build.dir}/bin/NHibernate.dll" /> <includes name="${build.dir}/bin/nunit.framework.dll" /> <includes name="${build.dir}/bin/log4net.dll" /> <includes name="${build.dir}/bin/Bamboo.Prevalence.dll" /> </references> </csc> </target> <target name="test" depends="build" description="run unit tests"> <copy file="prevalencecache.config" tofile="${build.dir}/bin/${nant.project.name}.Tests.dll.config" /> <nunit2 failonerror="false"> <formatter type="Xml" usefile="true" extension=".xml" outputdir="${build.dir}/bin" /> <test assemblyname="${build.dir}/bin/${nant.project.name}.Tests.dll" appconfig="${build.dir}/${nant.project.name}.Tests.dll.config" /> </nunit2> <if propertytrue="nunit2report.installed"> <mkdir dir="${build.dir}/testresults" /> <nunit2report out="${build.dir}/testresults/prevalencecache.html" todir="${build.dir}/testresults"> <fileset> <includes name="${build.dir}\bin\${nant.project.name}.Tests.dll-results.xml" /> </fileset> </nunit2report> </if> </target> </project> --- NEW FILE: PrevalenceCacheProvider.cs --- using System; using System.Collections; using System.Text; using log4net; using NHibernate.Cache; namespace NHibernate.Caches.Prevalence { /// <summary> /// Cache provider using <a href="http://bbooprevalence.sourceforge.net/">Bamboo Prevalence</a>, /// a Prevayler implementation in .NET. /// </summary> public class PrevalenceCacheProvider : ICacheProvider { private static readonly ILog log = LogManager.GetLogger( typeof( PrevalenceCacheProvider ) ); /// <summary> /// build and return a new cache implementation /// </summary> /// <param name="regionName"></param> /// <param name="properties"></param> /// <returns></returns> [CLSCompliant(false)] public ICache BuildCache( string regionName, IDictionary properties ) { if( regionName == null ) { regionName = ""; } if( properties == null ) { properties = new Hashtable(); } if( log.IsDebugEnabled ) { StringBuilder sb = new StringBuilder(); foreach( DictionaryEntry de in properties ) { sb.Append( "name=" ); sb.Append( de.Key.ToString() ); sb.Append( "&value=" ); sb.Append( de.Value.ToString() ); sb.Append( ";" ); } log.Debug( "building cache with region: " + regionName + ", properties: " + sb.ToString() ); } return new PrevalenceCache( regionName, properties ); } /// <summary></summary> /// <returns></returns> public long NextTimestamp() { return Timestamper.Next(); } } } --- NEW FILE: PrevalenceCacheFixture.cs --- using System; using System.Collections; using NHibernate.Cache; using NUnit.Framework; namespace NHibernate.Caches.Prevalence.Tests { [TestFixture] public class PrevalenceCacheFixture { private PrevalenceCacheProvider provider; private Hashtable props; [TestFixtureSetUp] public void FixtureSetup() { log4net.Config.DOMConfigurator.Configure(); props = new Hashtable(); provider = new PrevalenceCacheProvider(); } [Test] public void TestPut() { string key = "key1"; string value = "value"; ICache cache = provider.BuildCache( "nunit", props ); Assert.IsNotNull( cache, "no cache returned" ); Assert.IsNull( cache.Get( key ), "cache returned an item we didn't add !?!" ); cache.Put( key, value ); object item = cache.Get( key ); Assert.IsNotNull( item ); Assert.AreEqual( value, item, "didn't return the item we added" ); } [Test] public void TestRemove() { string key = "key1"; string value = "value"; ICache cache = provider.BuildCache( "nunit", props ); Assert.IsNotNull( cache, "no cache returned" ); // add the item cache.Put( key, value ); // make sure it's there object item = cache.Get( key ); Assert.IsNotNull( item, "item just added is not there" ); // remove it cache.Remove( key ); // make sure it's not there item = cache.Get( key ); Assert.IsNull( item, "item still exists in cache" ); } [Test] public void TestClear() { string key = "key1"; string value = "value"; ICache cache = provider.BuildCache( "nunit", props ); Assert.IsNotNull( cache, "no cache returned" ); // add the item cache.Put( key, value ); // make sure it's there object item = cache.Get( key ); Assert.IsNotNull( item, "couldn't find item in cache" ); // clear the cache cache.Clear(); // make sure we don't get an item item = cache.Get( key ); Assert.IsNull( item, "item still exists in cache" ); } [Test] public void TestDefaultConstructor() { ICache cache = new PrevalenceCache(); Assert.IsNotNull( cache ); } [Test] public void TestNoPropertiesConstructor() { ICache cache = new PrevalenceCache( "nunit" ); Assert.IsNotNull( cache ); } [Test] public void TestEmptyProperties() { ICache cache = new PrevalenceCache( "nunit", new Hashtable() ); Assert.IsNotNull( cache ); } [Test] [ExpectedException( typeof( ArgumentNullException ) )] public void TestNullKeyPut() { ICache cache = new PrevalenceCache(); cache.Put( null, null ); } [Test] [ExpectedException( typeof( ArgumentNullException ) )] public void TestNullValuePut() { ICache cache = new PrevalenceCache(); cache.Put( "nunit", null ); } [Test] public void TestNullKeyGet() { ICache cache = new PrevalenceCache(); cache.Put( "nunit", "value" ); object item = cache.Get( null ); Assert.IsNull( item ); } [Test] [ExpectedException( typeof( ArgumentNullException ) )] public void TestNullKeyRemove() { ICache cache = new PrevalenceCache(); cache.Remove( null ); } [Test] public void TestRegions() { string key = "key"; ICache cache1 = provider.BuildCache( "nunit1", props ); ICache cache2 = provider.BuildCache( "nunit2", props ); string s1 = "test1"; string s2 = "test2"; cache1.Put( key, s1 ); cache2.Put( key, s2 ); object get1 = cache1.Get( key ); object get2 = cache2.Get( key ); Assert.IsFalse( get1 == get2 ); } } } --- NEW FILE: prevalencecache.nunit --- <NUnitProject> <Settings activeconfig="Debug"/> <Config name="Debug"> <assembly path="bin\Debug\NHibernate.Caches.Prevalence.Tests.dll"/> </Config> <Config name="Release"> <assembly path="bin\Release\NHibernate.Caches.Prevalence.Tests.dll"/> </Config> </NUnitProject> --- NEW FILE: prevalencecache.config --- <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <nhibernate> <!-- <add key="hibernate.connection.pool_size" value="2" /> <add key="hibernate.use_outer_join" value="false" /> --> <!-- The valid strings for Isolation can be found in the documentation for the System.Data.IsolationLevel Enumeration documentation. Use the member names - not the values. --> <!--add key="hibernate.connection.isolation" value="ReadCommitted" /--> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.connection.connection_string" value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI" /> <add key="hibernate.cache.provider_class" value="NHibernate.Caches.Prevalence.PrevalenceCacheProvider,NHibernate.Caches.Prevalence" /> <add key="hibernate.cache.use_query_cache" value="true" /> </nhibernate> <log4net> <appender name="myFile" type="log4net.Appender.FileAppender,log4net" > <param name="File" value="log.txt" /> <param name="AppendToFile" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /> </layout> </appender> <root> <priority value="DEBUG" /> <appender-ref ref="myFile" /> </root> <logger name="NHibernate"> <level value="WARN" /> </logger> </log4net> </configuration> --- NEW FILE: PrevalenceCacheProviderFixture.cs --- using System; using System.Collections; using System.IO; using NHibernate.Cache; using NUnit.Framework; namespace NHibernate.Caches.Prevalence.Tests { [TestFixture] public class PrevalenceCacheProviderFixture { private PrevalenceCacheProvider provider; private Hashtable props; private string testDir; [TestFixtureSetUp] public void FixtureSetup() { log4net.Config.DOMConfigurator.Configure(); props = new Hashtable(); testDir = @"C:\temp\prevalence"; props.Add( "prevalenceBase", testDir ); } [SetUp] public void Setup() { provider = new PrevalenceCacheProvider(); } [TearDown] public void Teardown() { if( Directory.Exists( testDir ) ) { Directory.Delete( testDir ); } } [Test] public void TestBuildCacheNullNull() { ICache cache = provider.BuildCache( null, null ); Assert.IsNotNull( cache, "no cache returned" ); } [Test] public void TestBuildCacheStringNull() { ICache cache = provider.BuildCache( "a_region", null ); Assert.IsNotNull( cache, "no cache returned" ); } [Test] public void TestBuildCacheStringICollection() { Assert.IsFalse( Directory.Exists( testDir ) ); ICache cache = provider.BuildCache( "another_region", props ); Assert.IsTrue( Directory.Exists( testDir ) ); Assert.IsNotNull( cache, "no cache returned" ); } [Test] public void TestNextTimestamp() { long ts = provider.NextTimestamp(); Assert.IsNotNull( ts, "no timestamp returned" ); } } } --- NEW FILE: PrevalenceCache.cs --- using System; using System.Collections; using System.IO; using Bamboo.Prevalence; using log4net; using NHibernate.Cache; namespace NHibernate.Caches.Prevalence { /// <summary> /// Summary description for PrevalenceCache. /// </summary> public class PrevalenceCache : ICache, IDisposable { private static readonly ILog log = LogManager.GetLogger( typeof( PrevalenceCache ) ); private static readonly string _cacheKeyPrefix = "NHibernate-Cache:"; private string _region; private PrevalenceEngine _engine; private CacheSystem _system; /// <summary> /// default constructor /// </summary> public PrevalenceCache() : this( "nhibernate", null ) { } /// <summary> /// constructor with no properties /// </summary> /// <param name="region"></param> public PrevalenceCache( string region ) : this( region, null ) { } /// <summary> /// full constructor /// </summary> /// <param name="region"></param> /// <param name="properties">cache configuration properties</param> /// <remarks>There is only one configurable parameter: prevalenceBase. This is /// the directory on the file system where the Prevalence engine will save data. /// It can be relative to the current directory or a full path. If the directory /// doesn't exist, it will be created.</remarks> public PrevalenceCache( string region, IDictionary properties ) { _region = region; Configure( properties ); } private void Configure( IDictionary properties ) { string dataDir = Path.Combine( Environment.CurrentDirectory, _region ); if( properties != null ) { if( properties["prevalenceBase"] != null ) { string prevalenceBase = properties["prevalenceBase"].ToString(); if( Path.IsPathRooted( prevalenceBase ) ) { dataDir = prevalenceBase; } else { dataDir = Path.Combine( Environment.CurrentDirectory, prevalenceBase ); } } } if( Directory.Exists( dataDir ) == false ) { if( log.IsDebugEnabled ) { log.Debug( String.Format( "Data directory {0} doesn't exist: creating it.", dataDir ) ); } Directory.CreateDirectory( dataDir ); } if( log.IsDebugEnabled ) { log.Debug( String.Format( "configuring cache in {0}.", dataDir ) ); } _engine = PrevalenceActivator.CreateTransparentEngine( typeof( CacheSystem ), dataDir ); _system = _engine.PrevalentSystem as CacheSystem; } private string GetCacheKey( object key ) { return String.Concat( _cacheKeyPrefix, _region, ":", key.ToString() ); } /// <summary></summary> /// <param name="key"></param> /// <returns></returns> public object Get( object key ) { if( key == null ) { return null; } string cacheKey = GetCacheKey( key ); if( log.IsDebugEnabled ) { log.Debug( String.Format( "Fetching object '{0}' from the cache.", cacheKey ) ); } return _system.Get( cacheKey ); } /// <summary></summary> /// <param name="key"></param> /// <param name="value"></param> public void Put( object key, object value ) { if( key == null ) { if( log.IsErrorEnabled ) { log.Error( "null key passed to 'Put'" ); } throw new ArgumentNullException( "key", "null key not allowed" ); } if( value == null ) { if( log.IsErrorEnabled ) { log.Error( "null value passed to 'Put'" ); } throw new ArgumentNullException( "value", "null value not allowed" ); } string cacheKey = GetCacheKey( key ); if( log.IsDebugEnabled ) { log.Debug( String.Format( "setting value {1} for key {0}", cacheKey, value.ToString() ) ); } _system.Add( cacheKey, value ); } /// <summary></summary> /// <param name="key"></param> public void Remove( object key ) { if( key == null ) { if( log.IsErrorEnabled ) { log.Error( "null key passed to 'Remove'" ); } throw new ArgumentNullException( "key" ); } string cacheKey = GetCacheKey( key ); if( log.IsDebugEnabled ) { log.Debug( "removing item with key: " + cacheKey ); } _system.Remove( cacheKey ); } /// <summary></summary> public void Clear() { if( log.IsInfoEnabled ) { log.Info( "clearing all objects from system" ); } _system.Clear(); } /// <summary></summary> public void Destroy() { if( log.IsInfoEnabled ) { log.Info( "'Destroy' was called" ); } Clear(); } /// <summary></summary> public string Region { set { _region = value; } } #region IDisposable Members /// <summary> /// take snapshot before shutting down /// </summary> public void Dispose() { _engine.TakeSnapshot(); } #endregion } } |