class CacheContext : IDisposable

Coordinator
Apr 3, 2012 at 9:18 PM

using System;
using System.Runtime.Caching;

namespace Fox.SolarEnergy.ModelContext
{
    using Fox.Entities;
    using Fox.SolarEnergy.Models;

    /// <summary>
    /// Cache Context
    /// </summary>
    public class CacheContext : IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheContext"/> class.
        /// </summary>
        public CacheContext()
        {
            this._cache = new MemoryCache("ModelContextCache");
            this._solarEnergyReadingKey = "SolarEnergyReading";
            this._solarEnergyReadingMetaModelKey = "SolarEnergyReadingMetaModel";
        }

        /// <summary>
        /// Adds the specified reading.
        /// </summary>
        /// <param name="reading">The reading.</param>
        public void Add(SolarEnergyReading reading)
        {
            if (reading == null) throw new ArgumentNullException("reading", "The expected reading is not here.");

            var item = new CacheItem(this._solarEnergyReadingKey, reading);
            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = new DateTimeOffset(DateTime.Now, TimeSpan.FromMinutes(60))
            };

            this._cache.Add(item, policy);
        }

        /// <summary>
        /// Adds the specified meta data.
        /// </summary>
        /// <param name="metaData">The meta data.</param>
        public void Add(SolarEnergyReadingMetaModel metaData)
        {
            if (metaData == null) throw new ArgumentNullException("metaData", "The expected meta-data is not here.");

            var item = new CacheItem(this._solarEnergyReadingMetaModelKey, metaData);
            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = new DateTimeOffset(metaData.EarthToolsSunriseSunset.MorningSunrise)
            };

            this._cache.Add(item, policy);
        }

        /// <summary>
        /// Gets the solar energy reading.
        /// </summary>
        public SolarEnergyReading GetSolarEnergyReading()
        {
            var item = this._cache.GetCacheItem(this._solarEnergyReadingKey);
            return item.Value as SolarEnergyReading;
        }

        /// <summary>
        /// Gets the solar energy reading meta model.
        /// </summary>
        public SolarEnergyReadingMetaModel GetSolarEnergyReadingMetaModel()
        {
            var item = this._cache.GetCacheItem(this._solarEnergyReadingMetaModelKey);
            return item.Value as SolarEnergyReadingMetaModel;
        }

        #region IDisposable implementation:

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="CacheContext"/> is reclaimed by garbage collection.
        /// </summary>
        ~CacheContext()
        {
            this.Dispose(false);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    if (this._cache != null) this._cache.Dispose();
                }
                this._disposed = true;
            }
        }

        bool _disposed;

        #endregion

        MemoryCache _cache;

        string _solarEnergyReadingKey;
        string _solarEnergyReadingMetaModelKey;
    }
}