Files
Umbraco-CMS/src/Umbraco.Core/Cache/AppCacheExtensions.cs

71 lines
2.5 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Caching;
namespace Umbraco.Core.Cache
{
/// <summary>
/// Extensions for strongly typed access
/// </summary>
2019-01-18 08:14:08 +01:00
public static class AppCacheExtensions
2018-06-29 19:52:40 +02:00
{
2019-01-18 07:56:38 +01:00
public static T GetCacheItem<T>(this IAppPolicyCache provider,
2018-06-29 19:52:40 +02:00
string cacheKey,
Func<T> getCacheItem,
TimeSpan? timeout,
bool isSliding = false,
CacheItemPriority priority = CacheItemPriority.Normal,
CacheItemRemovedCallback removedCallback = null,
string[] dependentFiles = null)
{
var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding, priority, dependentFiles);
2018-06-29 19:52:40 +02:00
return result == null ? default(T) : result.TryConvertTo<T>().Result;
}
2019-01-18 07:56:38 +01:00
public static void InsertCacheItem<T>(this IAppPolicyCache provider,
2018-06-29 19:52:40 +02:00
string cacheKey,
Func<T> getCacheItem,
TimeSpan? timeout = null,
bool isSliding = false,
CacheItemPriority priority = CacheItemPriority.Normal,
CacheItemRemovedCallback removedCallback = null,
string[] dependentFiles = null)
{
provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, priority, dependentFiles);
2018-06-29 19:52:40 +02:00
}
2019-01-17 11:01:23 +01:00
public static IEnumerable<T> GetCacheItemsByKeySearch<T>(this IAppCache provider, string keyStartsWith)
2018-06-29 19:52:40 +02:00
{
2019-01-17 11:01:23 +01:00
var result = provider.SearchByKey(keyStartsWith);
2018-06-29 19:52:40 +02:00
return result.Select(x => x.TryConvertTo<T>().Result);
}
2019-01-17 11:01:23 +01:00
public static IEnumerable<T> GetCacheItemsByKeyExpression<T>(this IAppCache provider, string regexString)
2018-06-29 19:52:40 +02:00
{
2019-01-17 11:01:23 +01:00
var result = provider.SearchByRegex(regexString);
2018-06-29 19:52:40 +02:00
return result.Select(x => x.TryConvertTo<T>().Result);
}
2019-01-17 11:01:23 +01:00
public static T GetCacheItem<T>(this IAppCache provider, string cacheKey)
2018-06-29 19:52:40 +02:00
{
2019-01-17 11:01:23 +01:00
var result = provider.Get(cacheKey);
2018-06-29 19:52:40 +02:00
if (result == null)
{
return default(T);
}
return result.TryConvertTo<T>().Result;
}
2019-01-17 11:01:23 +01:00
public static T GetCacheItem<T>(this IAppCache provider, string cacheKey, Func<T> getCacheItem)
2018-06-29 19:52:40 +02:00
{
2019-01-17 11:01:23 +01:00
var result = provider.Get(cacheKey, () => getCacheItem());
2018-06-29 19:52:40 +02:00
if (result == null)
{
return default(T);
}
return result.TryConvertTo<T>().Result;
}
}
}