在IMemoryCache中存储和检索列表

时间:2019-05-20 08:46:52

标签: c# asp.net caching memory .net-core

我目前正在执行以下操作以将列表存储在IMemoryCache中:

_memoryCache.Set("PriceList", horsepricelist);

并访问它/返回数据:

public PriceResponse.PriceForEntry GetPriceListEntry(DateTime meetingDate, int courseId, int raceNumber, string horseCode)
{
    var pricelist = _memoryCache.Get("PriceList");
    var dateprefix = "/Date(" + meetingDate.Ticks.ToString() + ")/";

    return ((IEnumerable)pricelist).Cast<PriceResponse.PriceForEntry>().FirstOrDefault(x => x.meetingDate == dateprefix &&
                                                                                            x.courseId == courseId &&
                                                                                            x.raceNumber == raceNumber &&
                                                                                            x.horseCode == horseCode);
}

返回不包含马码的列表:

        public List<PriceResponse.PriceForEntry> GetPriceList(DateTime meetingDate, int courseId, int raceNumber, bool? ShowAll)
        {
           var pricelist = _memoryCache.Get<List<PriceResponse.PriceForEntry>>("PriceList");
           var dateprefix = "/Date(" + meetingDate.Ticks.ToString() + ")/";

           return pricelist.Where(x => x.meetingDate == dateprefix && x.courseId == courseId && x.raceNumber == raceNumber).ToList();
        }

保存在缓存中的列表的视图模型结构:

public class PriceResponse
{
    public class PriceForEntry
    {
        public string meetingDate { get; set; }
        public int courseId { get; set; }
        public int raceNumber { get; set; }
        public string horseCode { get; set; }
        public List<Bookmakerprice> bookmakerPrices { get; set; }
    }

    public class Bookmakerprice
    {
        public int bookmakerId { get; set; }
        public string bookmakerName { get; set; }
        public string selectionId { get; set; }
        public string fractionalOdds { get; set; }
        public float decimalOdds { get; set; }
        public string bookmakerRaceid { get; set; } //This is the event Id.
        public string bookmakerMarketId { get; set; } //Market Id.
    }
}

是否有更好的方法来获取值,例如在缓存中设置各种键和值以获取条目,而不是将对象转换回列表并遍历列表?

1 个答案:

答案 0 :(得分:0)

检查一下。 github repository中有这个助手和其他一些助手。

using System.Web.Caching;

namespace System.Web
{
    public static class CacheExtensions
    {
        static object _sync = new object();

        /// <summary>
        /// Executes a method and stores the result in cache using the given cache key.  If the data already exists in cache, it returns the data
        /// and doesn't execute the method.  Thread safe, although the method parameter isn't guaranteed to be thread safe.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache">Cache from HttpContext.  If null, method is executed directly.</param>
        /// <param name="cacheKey">Each method has it's own isolated set of cache items, so cacheKeys won't overlap across methods.</param>
        /// <param name="method"></param>
        /// <param name="expirationSeconds">Lifetime of cache items, in seconds</param>
        /// <returns></returns>
        public static T Data<T>(this Cache cache, string cacheKey, int expirationSeconds, Func<T> method)
        {
            var data = cache == null ? default(T) : (T)cache[cacheKey];
            if (data == null)
            {
                data = method();

                if (expirationSeconds > 0 && data != null)
                {
                    lock (_sync)
                    {
                        cache.Insert(cacheKey, data, null, DateTime.Now.AddSeconds(expirationSeconds), Cache.NoSlidingExpiration);
                    }
                }
            }
            return data;
        }
    }
}
相关问题