2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01: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
|
|
|
|
{
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public static T? GetCacheItem<T>(this IAppPolicyCache provider,
|
2018-06-29 19:52:40 +02:00
|
|
|
|
string cacheKey,
|
2021-12-16 13:44:20 +01:00
|
|
|
|
Func<T?> getCacheItem,
|
2018-06-29 19:52:40 +02:00
|
|
|
|
TimeSpan? timeout,
|
|
|
|
|
|
bool isSliding = false,
|
2021-12-16 13:44:20 +01:00
|
|
|
|
string[]? dependentFiles = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-11-07 18:34:05 +11: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,
|
2021-12-16 13:44:20 +01:00
|
|
|
|
string[]? dependentFiles = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-11-07 18:34:05 +11:00
|
|
|
|
provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-16 13:44:20 +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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-16 13:44:20 +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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|