PEPCache.cs
author Thomas
Mon, 25 Mar 2019 13:27:16 +0100
branchsync
changeset 2610 09fde2338362
parent 2555 6334e58eb55e
permissions -rw-r--r--
Merge with default

´╗┐using pEpCOMServerAdapterLib;
using System;
using System.Runtime.Caching;

namespace pEp
{
    /// <summary>
    /// The pEp cache.
    /// </summary>
    internal class PEPCache
    {
        /// <summary>
        /// The default memory cache.
        /// </summary>
        private static MemoryCache Cache
        {
            get { return MemoryCache.Default; }
        }

        #region Private Methods
        /// <summary>
        /// Adds an item to the pEp cache or updates an existing one.
        /// </summary>
        /// <param name="id">The identifier of the item to add.</param>
        /// <param name="item">The item to add.</param>
        /// <returns>True if added successfully, otherwise false.</returns>
        private static bool AddToOrUpdateCache(string id, object item)
        {
            bool success = false;

            if ((string.IsNullOrEmpty(id) == false) &&
                (item != null))
            {
                try
                {
                    PEPCache.Cache[id] = item;
                    success = true;
                }
                catch (Exception ex)
                {
                    success = false;
                    Log.Error("AddToCache: Error adding item to cache. " + ex.ToString());
                }
            }
            else
            {
                Log.Error("AddToCache: Error adding item to cache. Id or item are null.");
            }

            return success;
        }

        /// <summary>
        /// Gets an item from the pEp cache.
        /// </summary>
        /// <param name="id">The identifier of the item to retrieve.</param>
        /// <returns>The item or null if an error occured.</returns>
        private static object GetFromCache(string id)
        {
            object item = null;

            if (string.IsNullOrEmpty(id) == false)
            {
                try
                {
                    item = PEPCache.Cache?.Get(id);
                }
                catch (Exception ex)
                {
                    item = null;
                    Log.Error("GetFromCache: Error getting item from cache. " + ex.ToString());
                }
            }
            else
            {
                Log.Warning("GetFromCache: Error getting item from cache. Id is null.");
            }

            return item;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Adds a CacheItem to the pEp cache.
        /// </summary>
        /// <param name="id">The identifier of the item to add.</param>
        /// <param name="item">The CacheItem to add.</param>
        /// <returns>True if added successfully, otherwise false.</returns>
        public static bool AddItemToCache(string id, PEPCache.CacheItem item)
        {
            return PEPCache.AddToOrUpdateCache(id, item);
        }

        /// <summary>
        /// Gets a CacheItem from the pEp cache.
        /// </summary>
        /// <param name="id">The identifier of the item to retrieve.</param>
        /// <returns>The CacheItem or null if an error occured.</returns>
        public static CacheItem GetItemFromCache(string id)
        {
            return (PEPCache.GetFromCache(id) as PEPCache.CacheItem);
        }
        #endregion

        /// <summary>
        /// Container class for cache items.
        /// </summary>
        internal class CacheItem
        {
            public PEPMessage Mirror { get; set; } = null;
            public pEpRating Rating { get; set; } = pEpRating.pEpRatingUndefined;
        }
    }
}