Skip to content

Commit 4ca6a43

Browse files
author
Jeff Treuting
committed
try/catch around caching try and save methods
this will stop an unforeseen error from throwing an exception and not returning results at all, instead it will just default to hitting the DB
1 parent 4607d57 commit 4ca6a43

1 file changed

Lines changed: 80 additions & 16 deletions

File tree

SharpRepository.Repository/Caching/CachingStrategyBase.cs

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,52 +34,116 @@ public ICachingProvider CachingProvider
3434

3535
public virtual bool TryGetResult<TResult>(TKey key, Expression<Func<T, TResult>> selector, out TResult result)
3636
{
37-
return IsInCache(GetWriteThroughCacheKey(key, selector), out result);
37+
result = default(TResult);
38+
try
39+
{
40+
return IsInCache(GetWriteThroughCacheKey(key, selector), out result);
41+
}
42+
catch (Exception)
43+
{
44+
// don't let an error trying to find results stop everything, it should continue and then just go get the results from the DB itself
45+
return false;
46+
}
3847
}
3948

4049
public virtual void SaveGetResult<TResult>(TKey key, Expression<Func<T, TResult>> selector, TResult result)
4150
{
42-
SetCache(GetWriteThroughCacheKey(key, selector), result);
51+
try
52+
{
53+
SetCache(GetWriteThroughCacheKey(key, selector), result);
54+
}
55+
catch (Exception)
56+
{
57+
// don't let an error saving cache stop everything else
58+
}
4359
}
4460

4561
public virtual bool TryGetAllResult<TResult>(IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, out IEnumerable<TResult> result)
4662
{
47-
var cacheKey = GetAllCacheKey(queryOptions, selector);
48-
if (!IsInCache(cacheKey, out result))
49-
return false;
63+
result = null;
64+
try
65+
{
66+
var cacheKey = GetAllCacheKey(queryOptions, selector);
67+
if (!IsInCache(cacheKey, out result))
68+
return false;
5069

51-
// if there are no query options then we don't need to check for the cache for data to update them with
52-
return queryOptions == null || SetCachedQueryOptions(cacheKey, queryOptions);
70+
// if there are no query options then we don't need to check for the cache for data to update them with
71+
return queryOptions == null || SetCachedQueryOptions(cacheKey, queryOptions);
72+
}
73+
catch (Exception)
74+
{
75+
// don't let an error trying to find results stop everything, it should continue and then just go get the results from the DB itself
76+
return false;
77+
}
5378
}
5479

5580
public virtual void SaveGetAllResult<TResult>(IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, IEnumerable<TResult> result)
5681
{
57-
SetCache(GetAllCacheKey(queryOptions, selector), result, queryOptions);
82+
try
83+
{
84+
SetCache(GetAllCacheKey(queryOptions, selector), result, queryOptions);
85+
}
86+
catch (Exception)
87+
{
88+
// don't let an error saving cache stop everything else
89+
}
5890
}
5991

6092
public virtual bool TryFindAllResult<TResult>(ISpecification<T> criteria, IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, out IEnumerable<TResult> result)
6193
{
62-
var cacheKey = FindAllCacheKey(criteria, queryOptions, selector);
63-
if (!IsInCache(cacheKey, out result))
64-
return false;
94+
result = null;
95+
try
96+
{
97+
var cacheKey = FindAllCacheKey(criteria, queryOptions, selector);
98+
if (!IsInCache(cacheKey, out result))
99+
return false;
65100

66-
// if there are no query options then we don't need to check for the cache for data to update them with
67-
return queryOptions == null || SetCachedQueryOptions(cacheKey, queryOptions);
101+
// if there are no query options then we don't need to check for the cache for data to update them with
102+
return queryOptions == null || SetCachedQueryOptions(cacheKey, queryOptions);
103+
}
104+
catch (Exception)
105+
{
106+
// don't let an error trying to find results stop everything, it should continue and then just go get the results from the DB itself
107+
return false;
108+
}
68109
}
69110

70111
public virtual void SaveFindAllResult<TResult>(ISpecification<T> criteria, IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, IEnumerable<TResult> result)
71112
{
72-
SetCache(FindAllCacheKey(criteria, queryOptions, selector), result, queryOptions);
113+
try
114+
{
115+
SetCache(FindAllCacheKey(criteria, queryOptions, selector), result, queryOptions);
116+
}
117+
catch (Exception)
118+
{
119+
// don't let an error saving cache stop everything else;
120+
}
73121
}
74122

75123
public virtual bool TryFindResult<TResult>(ISpecification<T> criteria, IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, out TResult result)
76124
{
77-
return IsInCache(FindCacheKey(criteria, queryOptions, selector), out result);
125+
result = default(TResult);
126+
try
127+
{
128+
return IsInCache(FindCacheKey(criteria, queryOptions, selector), out result);
129+
}
130+
catch (Exception)
131+
{
132+
// don't let an error trying to find results stop everything, it should continue and then just go get the results from the DB itself
133+
return false;
134+
}
78135
}
79136

80137
public virtual void SaveFindResult<TResult>(ISpecification<T> criteria, IQueryOptions<T> queryOptions, Expression<Func<T, TResult>> selector, TResult result)
81138
{
82-
SetCache(FindCacheKey(criteria, queryOptions, selector), result);
139+
try
140+
{
141+
SetCache(FindCacheKey(criteria, queryOptions, selector), result);
142+
}
143+
catch (Exception)
144+
{
145+
// don't let an error saving cache stop everything else
146+
}
83147
}
84148

85149
public abstract void Add(TKey key, T result);

0 commit comments

Comments
 (0)