Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EasyCaching.Core/EasyCachingException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public EasyCachingException(string message)
: base(message)
{
}

public EasyCachingException(string message, Exception innerException)
: base(message, innerException)
{
}
}

public class EasyCachingNotFoundException : Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public override async Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey)
else
{
OnCacheMiss(cacheKey);
CheckResult(result);
return CacheValue<T>.NoValue;
}
}
Expand Down Expand Up @@ -132,7 +133,8 @@ public override async Task BaseRemoveAsync(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

await _memcachedClient.RemoveAsync(this.HandleCacheKey(cacheKey));
var data = await _memcachedClient.ExecuteRemoveAsync(this.HandleCacheKey(cacheKey));
CheckResult(data);
}

/// <summary>
Expand Down Expand Up @@ -199,6 +201,7 @@ public override async Task BaseRemoveByPrefixAsync(string prefix)
{
newValue = string.Concat(newValue, new Random().Next(9).ToString());
}

await _memcachedClient.StoreAsync(
Enyim.Caching.Memcached.StoreMode.Set,
this.HandleCacheKey(prefix),
Expand Down
37 changes: 28 additions & 9 deletions src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
using EasyCaching.Memcached.DistributedLock;
using EasyCaching.Memcached.DistributedLock;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -157,7 +157,11 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var result = ConvertFromStoredValue<T>(_memcachedClient.Get(this.HandleCacheKey(cacheKey)));
var data = _memcachedClient.PerformGet<object>(this.HandleCacheKey(cacheKey));

CheckResult(data);

var result = ConvertFromStoredValue<T>(data.Value);

if (result.HasValue)
{
Expand All @@ -180,7 +184,9 @@ public override void BaseRemove(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

_memcachedClient.Remove(this.HandleCacheKey(cacheKey));
var data = _memcachedClient.ExecuteRemove(this.HandleCacheKey(cacheKey));

CheckResult(data);
}

/// <summary>
Expand All @@ -203,11 +209,13 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}

_memcachedClient.Store(
var data = _memcachedClient.ExecuteStore(
Enyim.Caching.Memcached.StoreMode.Set,
this.HandleCacheKey(cacheKey),
this.ConvertToStoredValue(cacheValue),
expiration);

CheckResult(data);
}

/// <summary>
Expand Down Expand Up @@ -246,11 +254,14 @@ public override void BaseRemoveByPrefix(string prefix)
{
newValue = string.Concat(newValue, new Random().Next(9).ToString());
}
_memcachedClient.Store(
Enyim.Caching.Memcached.StoreMode.Set,
this.HandleCacheKey(prefix),
newValue,
new TimeSpan(0, 0, 0));

var data = _memcachedClient.ExecuteStore(
Enyim.Caching.Memcached.StoreMode.Set,
this.HandleCacheKey(prefix),
newValue,
new TimeSpan(0, 0, 0));

CheckResult(data);
}

/// <summary>
Expand Down Expand Up @@ -431,5 +442,13 @@ private void OnCacheMiss(string cacheKey)
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
}

private void CheckResult(Enyim.Caching.Memcached.Results.IOperationResult data)
{
if (!data.Success
&& (!data.InnerResult?.Success ?? false)
&& (data.InnerResult?.Message?.Contains("Failed to create socket") ?? false))
throw new EasyCachingException($"opereation fail, {data.InnerResult?.Message ?? ""}", data.Exception);
}
}
}
38 changes: 38 additions & 0 deletions test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,42 @@ public void Provider_Information_Should_Be_Correct()
Assert.Equal("mName", _provider.Name);
}
}

public class MemcachedProviderNoConnectionTest
{
[Fact]
public async void NoConnectionTest()
{
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddEasyCaching(option =>
{
option.UseMemcached(config =>
{
config.DBConfig = new EasyCachingMemcachedClientOptions
{
Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>
{
new Enyim.Caching.Configuration.Server() {Address = "123.123.123.123", Port = 45678 }
},
SocketPool = new Enyim.Caching.Configuration.SocketPoolOptions
{
ConnectionTimeout = TimeSpan.FromSeconds(2),
DeadTimeout = TimeSpan.FromSeconds(2),
ReceiveTimeout = TimeSpan.FromSeconds(2),
}
};
}, EasyCachingConstValue.DefaultMemcachedName);
});

IServiceProvider serviceProvider = services.BuildServiceProvider();
var factory = serviceProvider.GetService<IEasyCachingProviderFactory>();
var provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultMemcachedName);

Assert.Throws<EasyCachingException>(() => provider.Get<string>("123123"));
await Assert.ThrowsAnyAsync<EasyCachingException>(() => provider.GetAsync<string>("123123"));
Assert.Throws<EasyCachingException>(() => provider.Remove("123123"));
await Assert.ThrowsAnyAsync<EasyCachingException>(() => provider.RemoveAsync("123123"));
}
}
}