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

67 lines
2.2 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Cache;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Extensions
2018-06-29 19:52:40 +02:00
{
/// <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
{
public static T? GetCacheItem<T>(this IAppPolicyCache provider,
2018-06-29 19:52:40 +02:00
string cacheKey,
Func<T?> getCacheItem,
2018-06-29 19:52:40 +02:00
TimeSpan? timeout,
bool isSliding = false,
string[]? dependentFiles = null)
2018-06-29 19:52:40 +02:00
{
var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding, 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,
string[]? dependentFiles = null)
2018-06-29 19:52:40 +02:00
{
provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, dependentFiles);
2018-06-29 19:52:40 +02:00
}
2022-02-27 21:20:50 +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);
}
2022-01-13 09:27:37 +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);
}
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;
}
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;
}
}
}