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
3 changes: 2 additions & 1 deletion FastCache.Redis/Driver/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@
public Task<CacheItem> Get(string key)
{
var cacheValue = _redisClient.Get<CacheItem>(key);
if (cacheValue?.AssemblyName == null || cacheValue?.Type == null) return Task.FromResult(new CacheItem());
if (string.IsNullOrWhiteSpace(cacheValue?.AssemblyName) || string.IsNullOrWhiteSpace(cacheValue?.Type) ||
cacheValue?.Value == null) return Task.FromResult(new CacheItem());

var assembly = Assembly.Load(cacheValue.AssemblyName);
var valueType = assembly.GetType(cacheValue.Type, true, true);
cacheValue.Value = JsonConvert.DeserializeObject(cacheValue.Value as string, valueType);

Check warning on line 59 in FastCache.Redis/Driver/RedisCache.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'object? JsonConvert.DeserializeObject(string value, Type type)'.
return Task.FromResult(cacheValue);
}

Expand Down
11 changes: 11 additions & 0 deletions IntegrationTests/MultiSourceApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,15 @@ public async void TestMultiSourceEvictableAllowsEvictionByMultipleRules(string b
Assert.Equal("anson1", userDeleted.Name);
Assert.Equal(1, userDeleted.Age);
}

[Theory]
[InlineData("/MultiSource")]
public async void TestMultiSourceCacheableMultipleParam(string baseUrl)
{
var responseMessage = await _httpClient.GetAsync($"{baseUrl}/get/two?id=123&name=anson1");
Assert.Equal(responseMessage.StatusCode, HttpStatusCode.NoContent);

var responseMessage2 = await _httpClient.GetAsync($"{baseUrl}/get/two?id=123&name=anson1");
Assert.Equal(responseMessage2.StatusCode, HttpStatusCode.NoContent);
}
}
6 changes: 6 additions & 0 deletions TestApi/Controllers/MultiSourceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public virtual async Task<User> Get(string id)
{
return await _userService.Single(id);
}

[HttpGet("get/two")]
public virtual async Task<User> Get(string id, string name)
{
return await _userService.SingleOrDefault(id, name, true);
}

[HttpPost]
public User Add(User user)
Expand Down
2 changes: 2 additions & 0 deletions TestApi/Service/IMultiSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IMultiSourceService

Task<User> Single(string id);

Task<User?> SingleOrDefault(string id, string name, bool canChange);

Task<User?> SingleOrDefault(string id);

Task<User?> SingleOrDefaultByName(string name);
Expand Down
6 changes: 6 additions & 0 deletions TestApi/Service/MultiSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public virtual async Task<User> Single(string id)
return await dbContext.Set<User>().SingleAsync(x => x.Id == id);
}

[MultiSourceCacheable("MultiSource-single", "{id}:{name}", Target.Redis, 60)]
public virtual async Task<User?> SingleOrDefault(string id, string name, bool canChange)
{
return await dbContext.Set<User>().SingleOrDefaultAsync(x => x.Id == id && x.Name == name);
}

public virtual async Task<User> Update(User user)
{
var first = await dbContext.Set<User>().FirstAsync(x => x.Id == user.Id);
Expand Down
Loading