using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Cache;
namespace Umbraco.Extensions
{
///
/// Extensions for strongly typed access
///
public static class AppCacheExtensions
{
public static T? GetCacheItem(this IAppPolicyCache provider,
string cacheKey,
Func getCacheItem,
TimeSpan? timeout,
bool isSliding = false,
string[]? dependentFiles = null)
{
var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding, dependentFiles);
return result == null ? default(T) : result.TryConvertTo().Result;
}
public static void InsertCacheItem(this IAppPolicyCache provider,
string cacheKey,
Func getCacheItem,
TimeSpan? timeout = null,
bool isSliding = false,
string[]? dependentFiles = null)
{
provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, dependentFiles);
}
public static IEnumerable GetCacheItemsByKeySearch(this IAppCache provider, string keyStartsWith)
{
var result = provider.SearchByKey(keyStartsWith);
return result.Select(x => x.TryConvertTo().Result);
}
public static IEnumerable GetCacheItemsByKeyExpression(this IAppCache provider, string regexString)
{
var result = provider.SearchByRegex(regexString);
return result.Select(x => x.TryConvertTo().Result);
}
public static T? GetCacheItem(this IAppCache provider, string cacheKey)
{
var result = provider.Get(cacheKey);
if (result == null)
{
return default(T);
}
return result.TryConvertTo().Result;
}
public static T? GetCacheItem(this IAppCache provider, string cacheKey, Func getCacheItem)
{
var result = provider.Get(cacheKey, () => getCacheItem());
if (result == null)
{
return default(T);
}
return result.TryConvertTo().Result;
}
}
}