mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-19 17:51:45 +08:00
Implemented Redis-based cache provider.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using Newtonsoft.Json;
|
||||
using Orchard;
|
||||
using Orchard.Caching.Services;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Redis.Configuration;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
|
||||
namespace Orchard.Redis.Caching
|
||||
{
|
||||
[OrchardFeature("Orchard.Redis.Caching")]
|
||||
[OrchardSuppressDependency("Orchard.Caching.Services.DefaultCacheStorageProvider")]
|
||||
public class RedisCacheStorageProvider : Component, ICacheStorageProvider
|
||||
{
|
||||
public const string ConnectionStringKey = "Orchard.Redis.Cache";
|
||||
|
||||
private readonly ShellSettings _shellSettings;
|
||||
private readonly IRedisConnectionProvider _redisConnectionProvider;
|
||||
private readonly string _connectionString;
|
||||
|
||||
public IDatabase Database {
|
||||
get {
|
||||
return _redisConnectionProvider.GetConnection(_connectionString).GetDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
public RedisCacheStorageProvider(ShellSettings shellSettings, IRedisConnectionProvider redisConnectionProvider)
|
||||
{
|
||||
_shellSettings = shellSettings;
|
||||
_redisConnectionProvider = redisConnectionProvider;
|
||||
_connectionString = _redisConnectionProvider.GetConnectionString(ConnectionStringKey);
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public T Get<T>(string key) {
|
||||
var json = Database.StringGet(GetLocalizedKey(key));
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
public void Put<T>(string key, T value)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value);
|
||||
Database.StringSet(GetLocalizedKey(key), json, null);
|
||||
}
|
||||
|
||||
public void Put<T>(string key, T value, TimeSpan validFor)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value);
|
||||
Database.StringSet(GetLocalizedKey(key), json, validFor);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
Database.KeyDelete(key);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Database.KeyDeleteWithPrefix(GetLocalizedKey("*"));
|
||||
}
|
||||
|
||||
private string GetLocalizedKey(string key)
|
||||
{
|
||||
return _shellSettings.Name + ":Cache:" + key;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,3 +20,8 @@ Features:
|
||||
Description: An output cache storage provider using Redis.
|
||||
Category: Performance
|
||||
Dependencies: Orchard.OutputCache, Orchard.Redis
|
||||
Orchard.Redis.Caching:
|
||||
Name: Redis Cache
|
||||
Description: Business data cache using Redis.
|
||||
Category: Performance
|
||||
Dependencies: Orchard.Caching, Orchard.Redis
|
||||
|
||||
@@ -91,6 +91,10 @@
|
||||
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
|
||||
<Name>Orchard.Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Orchard.Caching\Orchard.Caching.csproj">
|
||||
<Project>{7528BF74-25C7-4ABE-883A-443B4EEC4776}</Project>
|
||||
<Name>Orchard.Caching</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Orchard.MessageBus\Orchard.MessageBus.csproj">
|
||||
<Project>{ed715544-e649-4f48-b8ee-9368c41c3ac0}</Project>
|
||||
<Name>Orchard.MessageBus</Name>
|
||||
@@ -101,6 +105,7 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Caching\RedisCacheStorageProvider.cs" />
|
||||
<Compile Include="Caching\RedisDatabaseExtensions.cs" />
|
||||
<Compile Include="Configuration\IRedisConnectionProvider.cs" />
|
||||
<Compile Include="Configuration\RedisConnectionProvider.cs" />
|
||||
|
||||
Reference in New Issue
Block a user