gpt4 book ai didi

c# - 在 IMemoryCache 中存储和检索列表

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:20 25 4
gpt4 key购买 nike

我目前正在执行以下操作以将列表存储在 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();
}

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

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.
}
}

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

最佳答案

检查一下。这个和其他一些助手在这个 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;
}
}
}

关于c# - 在 IMemoryCache 中存储和检索列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56217293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com