Skip to content

Commit

Permalink
Merge pull request dotnetcore#317 from dotnetcore/dev
Browse files Browse the repository at this point in the history
v1.4.0
  • Loading branch information
catcherwong authored Jul 23, 2021
2 parents b2f49c9 + 9474dce commit 76ccc15
Show file tree
Hide file tree
Showing 52 changed files with 1,516 additions and 176 deletions.
13 changes: 6 additions & 7 deletions build/releasenotes.props
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project>
<PropertyGroup>
<EasyCachingCorePackageNotes>
1. Allow to cache null value.
2. Ready for 1.x version.
1. Support distributed lock.
</EasyCachingCorePackageNotes>
<EasyCachingMemcachedPackageNotes>
1. Upgrading dependencies.
1. Support distributed lock.
</EasyCachingMemcachedPackageNotes>
<EasyCachingRedisPackageNotes>
1. Support SyncTimeout and AsyncTimeout (#282).
1. Support distributed lock.
</EasyCachingRedisPackageNotes>
<EasyCachingSQLitePackageNotes>
1. Upgrading dependencies.
Expand Down Expand Up @@ -38,7 +37,7 @@
1. Upgrading dependencies.
</EasyCachingProtobufPackageNotes>
<EasyCachingCSRedisPackageNotes>
1. Upgrading dependencies.
1. Support distributed lock.
</EasyCachingCSRedisPackageNotes>
<EasyCachingCSRedisBusPackageNotes>
1. Upgrading dependencies.
Expand All @@ -50,13 +49,13 @@
1. Upgrading dependencies.
</EasyCachingRabbitBusPackageNotes>
<EasyCachingDiskPackageNotes>
1. fixed disk provider tryset after expired (#280).
1. Upgrading dependencies.
</EasyCachingDiskPackageNotes>
<EasyCachingLiteDBPackageNotes>
1. Upgrading dependencies.
</EasyCachingLiteDBPackageNotes>
<EasyCachingSTJsonPackageNotes>
1. Init.
1. Upgrading dependencies.
</EasyCachingSTJsonPackageNotes>
</PropertyGroup>
</Project>
40 changes: 20 additions & 20 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project>
<PropertyGroup>
<EasyCachingCorePackageVersion>1.3.0</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.3.0</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.3.0</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.3.0</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.3.0</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.3.0</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.3.0</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.3.0</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.3.0</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.3.0</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.3.0</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.3.0</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.3.0</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.3.0</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.3.0</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.3.0</EasyCachingRabbitBusPackageVersion>
<EasyCachingDiskPackageVersion>1.3.0</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.3.0</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.3.0</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.3.0</EasyCachingSTJsonPackageVersion>
<EasyCachingCorePackageVersion>1.4.0</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.4.0</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.4.0</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.4.0</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.4.0</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.4.0</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.4.0</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.4.0</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.4.0</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.4.0</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.4.0</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.4.0</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.4.0</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.4.0</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.4.0</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.4.0</EasyCachingRabbitBusPackageVersion>
<EasyCachingDiskPackageVersion>1.4.0</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.4.0</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.4.0</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.4.0</EasyCachingSTJsonPackageVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace Microsoft.Extensions.DependencyInjection
using EasyCaching.CSRedis.DistributedLock;

namespace Microsoft.Extensions.DependencyInjection
{
using System;
using EasyCaching.Core;
using EasyCaching.Core.Configurations;
using EasyCaching.Core.DistributedLock;
using EasyCaching.CSRedis;
using Microsoft.Extensions.Configuration;
using System;

/// <summary>
/// EasyCaching options extensions.
Expand Down Expand Up @@ -61,5 +64,16 @@ void configure(RedisOptions x)
options.RegisterExtension(new RedisOptionsExtension(name, configure));
return options;
}

/// <summary>
/// Uses the CSRedis lock.
/// </summary>
/// <param name="options">Options.</param>
public static EasyCachingOptions UseCSRedisLock(this EasyCachingOptions options)
{
options.UseDistributedLock<CSRedisLockFactory>();

return options;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
using EasyCaching.Core;
using EasyCaching.Core.Configurations;
using EasyCaching.Core.DistributedLock;
using EasyCaching.Core.Serialization;
using EasyCaching.CSRedis.DistributedLock;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -86,8 +88,9 @@ public void AddServices(IServiceCollection services)
var serializers = x.GetServices<IEasyCachingSerializer>();
var optionsMon = x.GetRequiredService<IOptionsMonitor<RedisOptions>>();
var options = optionsMon.Get(_name);
var dlf = x.GetService<CSRedisLockFactory>();
var factory = x.GetService<ILoggerFactory>();
return new DefaultCSRedisCachingProvider(_name, clients, serializers, options, factory);
return new DefaultCSRedisCachingProvider(_name, clients, serializers, options, dlf, factory);
};

services.AddSingleton<IEasyCachingProvider, DefaultCSRedisCachingProvider>(createFactory);
Expand Down
31 changes: 27 additions & 4 deletions src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace EasyCaching.CSRedis
{
using System;
using System.Collections.Generic;
using System.Linq;
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
using EasyCaching.Core.Serialization;
using EasyCaching.CSRedis.DistributedLock;
using global::CSRedis;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class DefaultCSRedisCachingProvider : EasyCachingAbstractProvider
{
Expand Down Expand Up @@ -53,12 +55,33 @@ public partial class DefaultCSRedisCachingProvider : EasyCachingAbstractProvider
/// <param name="serializers">Serializers.</param>
/// <param name="options">Options.</param>
/// <param name="loggerFactory">Logger factory.</param>
public DefaultCSRedisCachingProvider(
string name,
IEnumerable<EasyCachingCSRedisClient> clients,
IEnumerable<IEasyCachingSerializer> serializers,
RedisOptions options,
ILoggerFactory loggerFactory = null)
: this(name, clients, serializers, options, null, loggerFactory)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.DefaultCSRedisCachingProvider"/> class.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="clients">Clients.</param>
/// <param name="serializers">Serializers.</param>
/// <param name="options">Options.</param>
/// <param name="factory">Distributed lock factory</param>
/// <param name="loggerFactory">Logger factory.</param>
public DefaultCSRedisCachingProvider(
string name,
IEnumerable<EasyCachingCSRedisClient> clients,
IEnumerable<IEasyCachingSerializer> serializers,
RedisOptions options,
CSRedisLockFactory factory = null,
ILoggerFactory loggerFactory = null)
: base(factory, options)
{
this._name = name;
this._options = options;
Expand Down Expand Up @@ -401,7 +424,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
/// <typeparam name="T">The 1st type parameter.</typeparam>
public override void BaseSetAll<T>(IDictionary<string, T> values, TimeSpan expiration)
{
//whether to use pipe based on redis mode
//whether to use pipe based on redis mode
if (MaxRdSecond > 0)
{
var addSec = new Random().Next(1, MaxRdSecond);
Expand Down
22 changes: 22 additions & 0 deletions src/EasyCaching.CSRedis/DistributedLock/CSRedisLockFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using EasyCaching.Core.DistributedLock;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;

namespace EasyCaching.CSRedis.DistributedLock
{
public class CSRedisLockFactory : DistributedLockFactory
{
private readonly IEnumerable<EasyCachingCSRedisClient> _clients;

public CSRedisLockFactory(IEnumerable<EasyCachingCSRedisClient> clients,
IOptionsMonitor<RedisOptions> optionsMonitor,
ILoggerFactory loggerFactory = null)
: base(name => DistributedLockOptions.FromProviderOptions(optionsMonitor.Get(name)), loggerFactory) =>
_clients = clients;

protected override IDistributedLockProvider GetLockProvider(string name) =>
new CSRedisLockProvider(name, _clients.Single(x => x.Name.Equals(name)));
}
}
42 changes: 42 additions & 0 deletions src/EasyCaching.CSRedis/DistributedLock/CSRedisLockProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CSRedis;
using EasyCaching.Core.DistributedLock;
using System;
using System.Threading.Tasks;

namespace EasyCaching.CSRedis.DistributedLock
{
public class CSRedisLockProvider : IDistributedLockProvider
{
private readonly string _name;
private readonly EasyCachingCSRedisClient _database;

public CSRedisLockProvider(string name, EasyCachingCSRedisClient database)
{
_name = name;
_database = database;
}

public Task<bool> SetAsync(string key, byte[] value, int ttlMs) =>
_database.SetAsync($"{_name}/{key}", value, TimeSpan.FromMilliseconds(ttlMs));

public bool Add(string key, byte[] value, int ttlMs) =>
_database.Set($"{_name}/{key}", value, TimeSpan.FromMilliseconds(ttlMs), RedisExistence.Nx);

public Task<bool> AddAsync(string key, byte[] value, int ttlMs) =>
_database.SetAsync($"{_name}/{key}", value, TimeSpan.FromMilliseconds(ttlMs), RedisExistence.Nx);

public bool Delete(string key, byte[] value) =>
(long)_database.Eval(@"if redis.call('GET', KEYS[1]) == ARGV[1] then
return redis.call('DEL', KEYS[1]);
end
return -1;", $"{_name}/{key}", value) >= 0;

public async Task<bool> DeleteAsync(string key, byte[] value) =>
(long)await _database.EvalAsync(@"if redis.call('GET', KEYS[1]) == ARGV[1] then
return redis.call('DEL', KEYS[1]);
end
return -1;", $"{_name}/{key}", value) >= 0;

public bool CanRetry(Exception ex) => ex is RedisClientException;
}
}
Loading

0 comments on commit 76ccc15

Please sign in to comment.