Closed kooshanabedian closed 4 years ago
Note that the Id property is located on an aggregate base class and inherited by other classes, if it matters in any way.
I can't reproduce it. Try this sample.
Using 2 different parameters will create 2 different cache-keys with different results from the cache:
Indeed. Here is my sample for three different values for Id parameter. It generates three different records in Redis but all of them referencing the same hash.
Records:
1)
2)
3)
Data:
You are using V2.6.3. Please update it to 2.6.4, because it considers ConstantExpression
value's in final hash. In this case you won't see the same cache-keys.
You are right. my bad. updating to 2.64. fixes the issue for cases where I directly use DbContext. If I use some extension methods on DbContext to get some form of a repository pattern, I'm stuck again. But I guess that's some misuse on my part.
public static async Task<TEntity> GetFirstOrDefaultAsync<TEntity,TDbContext>(this TDbContext dbContext, Expression<Func<TEntity, bool>>? predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null,
bool disableTracking = true,
CancellationToken cancellationToken = default(CancellationToken))
where TEntity : class
where TDbContext : CacheawareDbContext
{
IQueryable<TEntity> query = dbContext.Set<TEntity>();
if (disableTracking)
{
query = query.AsNoTracking();
}
if (include != null)
{
query = include(query);
}
if (predicate != null)
{
query = query.Where(predicate);
}
if (orderBy != null)
{
return await orderBy(query)
.Cacheable(cacheExpirationMode,timeSpan.Value)
.FirstOrDefaultAsync(cancellationToken);
}
else
{
return await query
.Cacheable(cacheExpirationMode,timeSpan.Value)
.FirstOrDefaultAsync(cancellationToken);
}
}
I tested your ext method and it works fine (creates 2 different cache-keys for 2 different inputs):
Thank you for your help. I do very much appreciate it. I'm closing this now, since I couldn't track the core issue, which is obviously on my par.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related problems.
Summary of the issue
It seems that always the first cached result is returned.
Environment
Example code/Steps to reproduce:
pretty basic setup indeed, with redis cache.