コード例 #1
0
        /// <summary>
        /// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
        /// </summary>
        /// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
        /// <param name="cache">The <see cref="CacheBase"/> used for this strategy.</param>
        /// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
        public static ICacheConcurrencyStrategy CreateCache(string usage, CacheBase cache)
        {
            if (log.IsDebugEnabled())
            {
                log.Debug("cache for: {0} usage strategy: {1}", cache.RegionName, usage);
            }

            ICacheConcurrencyStrategy ccs;

            switch (usage)
            {
            case ReadOnly:
                ccs = new ReadOnlyCache();
                break;

            case ReadWrite:
                ccs = new ReadWriteCache();
                break;

            case NonstrictReadWrite:
                ccs = new NonstrictReadWriteCache();
                break;

            //case CacheFactory.Transactional:
            //	ccs = new TransactionalCache();
            //	break;
            default:
                throw new MappingException(
                          "cache usage attribute should be read-write, read-only or nonstrict-read-write");
            }

            ccs.Cache = cache;

            return(ccs);
        }
コード例 #2
0
		/// <summary>
		/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
		/// </summary>
		/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
		/// <param name="name">The name of the class the strategy is being created for.</param>
		/// <param name="mutable"><c>true</c> if the object being stored in the cache is mutable.</param>
		/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
		// was private in h2.1
		public static ICacheConcurrencyStrategy CreateCache( string usage, string name, bool mutable )
		{
			if( log.IsDebugEnabled )
			{
				log.Debug( string.Format( "cache for: {0} usage strategy: {1}", name, usage ) );
			}

			ICacheConcurrencyStrategy ccs = null;
			switch( usage )
			{
				case CacheFactory.ReadOnly:
					if( mutable )
					{
						log.Warn( "read-only cache configured for mutable: " + name );
					}
					ccs = new ReadOnlyCache();
					break;
				case CacheFactory.ReadWrite:
					ccs = new ReadWriteCache();
					break;
				case CacheFactory.NonstrictReadWrite:
					ccs = new NonstrictReadWriteCache();
					break;
				//case CacheFactory.Transactional:
				//	ccs = new TransactionalCache();
				//	break;
				default:
					throw new MappingException( "cache usage attribute should be read-write, read-only, nonstrict-read-write, or transactional" );
			}

			return ccs;
		}
コード例 #3
0
        /// <summary>
        /// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
        /// </summary>
        /// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
        /// <param name="name">The name of the class the strategy is being created for.</param>
        /// <param name="mutable"><see langword="true" /> if the object being stored in the cache is mutable.</param>
        /// <param name="settings">Used to retrieve the global cache region prefix.</param>
        /// <param name="properties">Properties the cache provider can use to configure the cache.</param>
        /// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
        public static ICacheConcurrencyStrategy CreateCache(string usage, string name, bool mutable, Settings settings,
                                                            IDictionary <string, string> properties)
        {
            if (usage == null || !settings.IsSecondLevelCacheEnabled)
            {
                return(null);                                                                  //no cache
            }
            string prefix = settings.CacheRegionPrefix;

            if (prefix != null)
            {
                name = prefix + '.' + name;
            }

            if (log.IsDebugEnabled())
            {
                log.Debug("cache for: {0} usage strategy: {1}", name, usage);
            }

            ICacheConcurrencyStrategy ccs;

            switch (usage)
            {
            case ReadOnly:
                if (mutable)
                {
                    log.Warn("read-only cache configured for mutable: {0}", name);
                }
                ccs = new ReadOnlyCache();
                break;

            case ReadWrite:
                ccs = new ReadWriteCache();
                break;

            case NonstrictReadWrite:
                ccs = new NonstrictReadWriteCache();
                break;

            //case CacheFactory.Transactional:
            //	ccs = new TransactionalCache();
            //	break;
            default:
                throw new MappingException(
                          "cache usage attribute should be read-write, read-only, nonstrict-read-write, or transactional");
            }

            try
            {
                ccs.Cache = settings.CacheProvider.BuildCache(name, properties);
            }
            catch (CacheException e)
            {
                throw new HibernateException("Could not instantiate cache implementation", e);
            }

            return(ccs);
        }
コード例 #4
0
		/// <summary>
		/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
		/// </summary>
		/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
		/// <param name="name">The name of the class the strategy is being created for.</param>
		/// <param name="mutable"><see langword="true" /> if the object being stored in the cache is mutable.</param>
		/// <param name="settings">Used to retrieve the global cache region prefix.</param>
		/// <param name="properties">Properties the cache provider can use to configure the cache.</param>
		/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
		public static ICacheConcurrencyStrategy CreateCache(string usage, string name, bool mutable, Settings settings,
		                                                    IDictionary<string,string> properties)
		{
			if (usage == null || !settings.IsSecondLevelCacheEnabled) return null; //no cache

			string prefix = settings.CacheRegionPrefix;
			if (prefix != null) name = prefix + '.' + name;

			if (log.IsDebugEnabled)
			{
				log.Debug(string.Format("cache for: {0} usage strategy: {1}", name, usage));
			}

			ICacheConcurrencyStrategy ccs;
			switch (usage)
			{
				case ReadOnly:
					if (mutable)
					{
						log.Warn("read-only cache configured for mutable: " + name);
					}
					ccs = new ReadOnlyCache();
					break;
				case ReadWrite:
					ccs = new ReadWriteCache();
					break;
				case NonstrictReadWrite:
					ccs = new NonstrictReadWriteCache();
					break;
					//case CacheFactory.Transactional:
					//	ccs = new TransactionalCache();
					//	break;
				default:
					throw new MappingException(
						"cache usage attribute should be read-write, read-only, nonstrict-read-write, or transactional");
			}

			ICache impl;
			try
			{
				impl = settings.CacheProvider.BuildCache(name, properties);
			}
			catch (CacheException e)
			{
				throw new HibernateException("Could not instantiate cache implementation", e);
			}
			ccs.Cache = impl;

			return ccs;
		}
コード例 #5
0
        /// <summary>
        /// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
        /// </summary>
        /// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
        /// <param name="name">The name of the class the strategy is being created for.</param>
        /// <param name="mutable"><c>true</c> if the object being stored in the cache is mutable.</param>
        /// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
        // was private in h2.1
        public static ICacheConcurrencyStrategy CreateCache(string usage, string name, bool mutable)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(string.Format("cache for: {0} usage strategy: {1}", name, usage));
            }

            ICacheConcurrencyStrategy ccs = null;

            switch (usage)
            {
            case CacheFactory.ReadOnly:
                if (mutable)
                {
                    log.Warn("read-only cache configured for mutable: " + name);
                }
                ccs = new ReadOnlyCache();
                break;

            case CacheFactory.ReadWrite:
                ccs = new ReadWriteCache();
                break;

            case CacheFactory.NonstrictReadWrite:
                ccs = new NonstrictReadWriteCache();
                break;

            //case CacheFactory.Transactional:
            //	ccs = new TransactionalCache();
            //	break;
            default:
                throw new MappingException("cache usage attribute should be read-write, read-only, nonstrict-read-write, or transactional");
            }

            return(ccs);
        }
コード例 #6
0
ファイル: CacheFixture.cs プロジェクト: paulbatum/nhibernate
		public void DoTestCache(ICacheProvider cacheProvider)
		{
			ICache cache = cacheProvider.BuildCache(typeof(String).FullName, new Dictionary<string, string>());

			long longBefore = Timestamper.Next();

			Thread.Sleep(15);

			long before = Timestamper.Next();

			Thread.Sleep(15);

			ICacheConcurrencyStrategy ccs = new ReadWriteCache();
			ccs.Cache = cache;

			// cache something
			CacheKey fooKey = CreateCacheKey("foo");

			Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false));

			Thread.Sleep(15);

			long after = Timestamper.Next();

			Assert.IsNull(ccs.Get(fooKey, longBefore));
			Assert.AreEqual("foo", ccs.Get(fooKey, after));
			Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false));

			// update it;

			ISoftLock fooLock = ccs.Lock(fooKey, null);

			Assert.IsNull(ccs.Get(fooKey, after));
			Assert.IsNull(ccs.Get(fooKey, longBefore));
			Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false));

			Thread.Sleep(15);

			long whileLocked = Timestamper.Next();

			Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false));

			Thread.Sleep(15);

			ccs.Release(fooKey, fooLock);

			Assert.IsNull(ccs.Get(fooKey, after));
			Assert.IsNull(ccs.Get(fooKey, longBefore));
			Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false));
			Assert.IsFalse(ccs.Put(fooKey, "bar", after, null, null, false));

			Thread.Sleep(15);

			long longAfter = Timestamper.Next();

			Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false));
			Assert.IsNull(ccs.Get(fooKey, after));
			Assert.IsNull(ccs.Get(fooKey, whileLocked));

			Thread.Sleep(15);

			long longLongAfter = Timestamper.Next();

			Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter));

			// update it again, with multiple locks

			ISoftLock fooLock1 = ccs.Lock(fooKey, null);
			ISoftLock fooLock2 = ccs.Lock(fooKey, null);

			Assert.IsNull(ccs.Get(fooKey, longLongAfter));

			Thread.Sleep(15);

			whileLocked = Timestamper.Next();

			Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false));

			Thread.Sleep(15);

			ccs.Release(fooKey, fooLock2);

			Thread.Sleep(15);

			long betweenReleases = Timestamper.Next();

			Assert.IsFalse(ccs.Put(fooKey, "bar", betweenReleases, null, null, false));
			Assert.IsNull(ccs.Get(fooKey, betweenReleases));

			Thread.Sleep(15);

			ccs.Release(fooKey, fooLock1);

			Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false));

			Thread.Sleep(15);

			longAfter = Timestamper.Next();

			Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false));
			Assert.IsNull(ccs.Get(fooKey, whileLocked));

			Thread.Sleep(15);

			longLongAfter = Timestamper.Next();

			Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter));
		}
コード例 #7
0
ファイル: CacheFixture.cs プロジェクト: paulbatum/nhibernate
		public void MinValueTimestamp()
		{
			ICache cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>());
			ICacheConcurrencyStrategy strategy = new ReadWriteCache();
			strategy.Cache = cache;

			DoTestMinValueTimestampOnStrategy(cache, new ReadWriteCache());
			DoTestMinValueTimestampOnStrategy(cache, new NonstrictReadWriteCache());
			DoTestMinValueTimestampOnStrategy(cache, new ReadOnlyCache());
		}