Skip to content

Commit 781f539

Browse files
author
Jeff Treuting
committed
Caching and Paging Options issue fixed
Fixes #74 The logic for caching TotalItems was checking for PagingOptions<T> so if you were using the expression based PagingOptions<T, TSortKey> then it wouldn't cache the TotalItems and then would return 0 the next time. I added the IPagingOptions interface that both PagingOptions versions inherit from and so I can just check for this interface and access the TotalItems off of it. And that fixes the issue.
1 parent 329142b commit 781f539

6 files changed

Lines changed: 26 additions & 15 deletions

File tree

SharpRepository.Repository/Caching/CachingStrategyBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ protected bool SetCachedQueryOptions(string cacheKey, IQueryOptions<T> queryOpti
204204
// originally was thinking doing a ref arg for queryOptions and setting it via cache but ran into an issue in QueryManager using a ref in a lamda expression
205205

206206
// we only need to do this for PagingOptions because it has a TotalItems property that we need
207-
if (!(queryOptions is PagingOptions<T>))
207+
if (!(queryOptions is IPagingOptions))
208208
return true;
209209

210210
int totalItems;
211211
// there is a PagingOptions passed in so we want to make sure that both the results and the queryOptions are in cache
212212
// this is a safety in case the caching provider kicked one of them out
213213
if (IsPagingTotalInCache(cacheKey, out totalItems))
214214
{
215-
((PagingOptions<T>)queryOptions).TotalItems = totalItems;
215+
((IPagingOptions)queryOptions).TotalItems = totalItems;
216216
return true;
217217
}
218218

@@ -238,9 +238,9 @@ protected void SetCache<TCacheItem>(string cacheKey, TCacheItem result, IQueryOp
238238
{
239239
CachingProvider.Set(cacheKey, result);
240240

241-
if (queryOptions is PagingOptions<T>)
241+
if (queryOptions is IPagingOptions)
242242
{
243-
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems);
243+
CachingProvider.Set(cacheKey + "=>pagingTotal", ((IPagingOptions)queryOptions).TotalItems);
244244
}
245245
//Trace.WriteLine(String.Format("Write item to cache: {0} - {1}", cacheKey, typeof(TCacheItem).Name));
246246
}

SharpRepository.Repository/Caching/CompoundKeyCachingStrategyCommon.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected bool SetCachedQueryOptions(string cacheKey, IQueryOptions<T> queryOpti
122122
// originally was thinking doing a ref arg for queryOptions and setting it via cache but ran into an issue in QueryManager using a ref in a lamda expression
123123

124124
// we only need to do this for PagingOptions because it has a TotalItems property that we need
125-
if (!(queryOptions is PagingOptions<T>))
125+
if (!(queryOptions is IPagingOptions))
126126
return true;
127127

128128
int totalItems;
@@ -156,9 +156,9 @@ protected void SetCache<TCacheItem>(string cacheKey, TCacheItem result, IQueryOp
156156
{
157157
CachingProvider.Set(cacheKey, result);
158158

159-
if (queryOptions is PagingOptions<T>)
159+
if (queryOptions is IPagingOptions)
160160
{
161-
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems);
161+
CachingProvider.Set(cacheKey + "=>pagingTotal", ((IPagingOptions)queryOptions).TotalItems);
162162
}
163163
//Trace.WriteLine(String.Format("Write item to cache: {0} - {1}", cacheKey, typeof(TCacheItem).Name));
164164
}

SharpRepository.Repository/Caching/TimeoutCachingStrategyBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public override void Save()
4646
{
4747
CachingProvider.Set(cacheKey, result, CacheItemPriority.Default, TimeoutInSeconds);
4848

49-
if (queryOptions is PagingOptions<T>)
49+
if (queryOptions is IPagingOptions)
5050
{
51-
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems, CacheItemPriority.Default, TimeoutInSeconds);
51+
CachingProvider.Set(cacheKey + "=>pagingTotal", ((IPagingOptions)queryOptions).TotalItems, CacheItemPriority.Default, TimeoutInSeconds);
5252
}
5353
}
5454
catch (Exception)

SharpRepository.Repository/Caching/TimeoutCompoundKeyCachingStrategyBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void Save()
4545
{
4646
CachingProvider.Set(cacheKey, result, CacheItemPriority.Default, TimeoutInSeconds);
4747

48-
if (queryOptions is PagingOptions<T>)
48+
if (queryOptions is IPagingOptions)
4949
{
5050
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems, CacheItemPriority.Default, TimeoutInSeconds);
5151
}
@@ -110,7 +110,7 @@ public override void Save()
110110
{
111111
CachingProvider.Set(cacheKey, result, CacheItemPriority.Default, TimeoutInSeconds);
112112

113-
if (queryOptions is PagingOptions<T>)
113+
if (queryOptions is IPagingOptions)
114114
{
115115
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems, CacheItemPriority.Default, TimeoutInSeconds);
116116
}
@@ -175,7 +175,7 @@ public override void Save()
175175
{
176176
CachingProvider.Set(cacheKey, result, CacheItemPriority.Default, TimeoutInSeconds);
177177

178-
if (queryOptions is PagingOptions<T>)
178+
if (queryOptions is IPagingOptions)
179179
{
180180
CachingProvider.Set(cacheKey + "=>pagingTotal", ((PagingOptions<T>)queryOptions).TotalItems, CacheItemPriority.Default, TimeoutInSeconds);
181181
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace SharpRepository.Repository.Queries
2+
{
3+
public interface IPagingOptions
4+
{
5+
int PageSize { get; set; }
6+
int PageNumber { get; set; }
7+
int Skip { get; }
8+
int Take { get; }
9+
int TotalItems { get; set; }
10+
}
11+
}

SharpRepository.Repository/Queries/PagingOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace SharpRepository.Repository.Queries
99
/// </summary>
1010
/// <typeparam name="T">The entity type of the repository.</typeparam>
1111
/// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
12-
public class PagingOptions<T, TSortKey> : SortingOptions<T, TSortKey>
12+
public class PagingOptions<T, TSortKey> : SortingOptions<T, TSortKey>, IPagingOptions
1313
{
1414
public int PageSize { get; set; }
1515
public int PageNumber { get; set; }
1616
public int Skip { get { return (PageNumber - 1) * PageSize; } }
1717
public int Take { get { return PageSize; } }
18-
public int TotalItems { get; internal set; }
18+
public int TotalItems { get; set; }
1919

2020
public PagingOptions(int pageNumber, int pageSize, Expression<Func<T, TSortKey>> sortExpression, bool isDescending = false)
2121
: base(sortExpression, isDescending)
@@ -64,7 +64,7 @@ public override string ToString()
6464
/// Used to define the paging criteria on queries run against a repository.
6565
/// </summary>
6666
/// <typeparam name="T">The entity type of the repository.</typeparam>
67-
public class PagingOptions<T> : SortingOptions<T>
67+
public class PagingOptions<T> : SortingOptions<T>, IPagingOptions
6868
{
6969
public int PageSize { get; set; }
7070
public int PageNumber { get; set; }

0 commit comments

Comments
 (0)