#region Apache Notice
/*****************************************************************************
* $Revision: 575913 $
* $LastChangedDate$
* $LastChangedBy$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/2005 - The Apache Software Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************/
#endregion
#region Using
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.Common.Exceptions;
using Apache.Ibatis.Common.Logging;
using Apache.Ibatis.Common.Logging.Impl;
using Apache.Ibatis.Common.Utilities;
using Apache.Ibatis.DataMapper.MappedStatements;
using Apache.Ibatis.DataMapper.Model.Cache.Decorators;
#endregion
namespace Apache.Ibatis.DataMapper.Model.Cache
{
///
/// Summary description for CacheModel.
///
[Serializable]
[DebuggerDisplay("CacheModel: {Id}-{Cache}")]
public class CacheModel
{
private ICache cache = null;
private readonly IList statementFlushNames = new List();
///
/// This is used to represent null objects that are returned from the cache so
/// that they can be cached, too.
///
public readonly static object NULL_OBJECT = new Object();
///
/// Identifier used to identify the CacheModel amongst the others.
///
public string Id
{
get { return cache.Id; }
}
///
/// Gets the statement flush on execute names.
///
/// The statement flush names.
public IList StatementFlushNames
{
get { return statementFlushNames; }
}
///
/// Set the cache implementation
///
public ICache Cache
{
get { return cache; }
set { cache = value; }
}
///
/// Adds an item with the specified key and value into cached data.
/// Gets a cached object with the specified key.
///
/// The cached object or null
public object this[CacheKey key]
{
get { return cache[key]; }
set { cache[key] = value;}
}
///
/// Initializes a new instance of the class.
///
/// The id.
/// The cache implementation.
/// The flush interval.
/// The size.
/// if set to true [is share].
public CacheModel(
string id,
string implementation,
long flushInterval,
int size,
bool isShare)
{
Contract.Require.That(id, Is.Not.Null & Is.Not.Empty).When("retrieving argument id in CacheModel constructor");
Contract.Require.That(implementation, Is.Not.Null & Is.Not.Empty).When("retrieving argument implementation in CacheModel constructor");
try
{
// Build the Cache
Type type = TypeUtils.ResolveType(implementation);
object[] arguments = new object[0];
cache = (ICache)Activator.CreateInstance(type, arguments);
cache.Id = id;
cache = new NullValueDecorator(cache);
if (size != int.MinValue)
{
cache.Size = size;
}
if (flushInterval != long.MinValue)
{
cache = cache = new ScheduledCache(cache, flushInterval);
}
if (isShare)
{
cache = cache = new SharedCache(cache);
}
if (!(LogManager.Adapter is NoOpLoggerFA))
{
cache = new LoggingCache(cache);
}
cache = new SynchronizedCache(cache);
}
catch (Exception e)
{
throw new ConfigurationException("Error instantiating cache implementation for cache '"+id+". Cause: " + e.Message, e);
}
}
///
/// Event listener
///
/// A MappedStatement on which we listen ExecuteEventArgs event.
public void RegisterTriggerStatement(IMappedStatement mappedStatement)
{
mappedStatement.Executed +=FlushHandler;
}
///
/// FlushHandler which clear the cache
///
///
///
private void FlushHandler(object sender, ExecuteEventArgs e)
{
cache.Clear();
}
}
}