-
-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support GetOrAdd and TryGetOrAdd for CacheItem #160
Support GetOrAdd and TryGetOrAdd for CacheItem #160
Conversation
Hi @ahockersten Regarding the implementation, I think the overloads for CacheItem should also have a value factory, as the purpose of those methods is lazy evaluation of the object creation in case it doesn't exist. Meaning, the factory gets called only if Get(key) doesn't yield any result. In your implementation, you'd always have to provide a constructed cache item, even if it is not needed. I think the interface should be more like
(plus the corresponding overloads without region) You could refactor the existing Hope that makes sense and happy coding :> |
1d6ee57
to
439c22a
Compare
True, there should be a ValueFactory version. I'm wondering if there should be a version that just takes a value as well, or if you think that's unnecessary? |
I would say it is not necessary to have one without the factory |
Actually, now I remember why I didn't add a ValueFactory version in my original commit. If you do, then you have to create CacheItems with no value set. That seems like it would encourage a potentially bad practice, as it would mean Consider this example:
The case where there is already is an item in the cache is uninteresting, so let's not consider that. Now that I have written out the question, it seems like the answer doesn't really matter, because CacheManager doesn't use CacheItems in a way that would make it matter (I think?), and if you are using CacheItems as anything other than an interface for communicating with CacheManager then you're on your own. Did the above explanation make any sense, or am I mostly explaining away things to myself? :) Btw, thank you for the Redis setup instructions. |
Also, I should have read your interface proposal, as it neatly rounds the potential non-problem I described above. 🤦♂️ |
Hehe no worries, always good to talk about those things to get to a better understanding ;) My interface suggestion had the key and region as parameters as that's what you'd need to do the Regarding the return value, I guess it would actually make more sense to return the CacheItem instead of the value only => in case you need it, you don't have to make another call to |
6fda8e9
to
171cc73
Compare
I think everything should be in order now. As you suggested, I return the CacheItem rather than the value for the functions that expect CacheItems to be passed into them. |
171cc73
to
c77f7c6
Compare
Or so I thought, at least. Weird. Tests didn't fail on my computer last night. Will look at this when I get home tonight. |
c77f7c6
to
3d19bf1
Compare
I found the remaining issues and fixed them. |
3d19bf1
to
8a2f43f
Compare
Or so I thought anyway. Some tests are failing, but it seems that was a preexisting problem? BTW, I made a change to the behavior of GetOrAdd(), which meant I had to make a change to the GetOrAdd_AddNull() test. GetOrAdd() now throws an ArgumentNullException if the key is null, previously it would throw an InvalidOperationException(). Technically this is a change in the API, I suppose. I'll note that the docs say GetOrAdd() should throw an ArgumentException on invalid arguments, so documentation-wise it seems this behavior could be considered a bugfix. |
@ahockersten I'm currently busy with other things, I will review it later this week |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, just a few minor things
@@ -20,7 +20,7 @@ public TCacheValue GetOrAdd(string key, Func<string, TCacheValue> valueFactory) | |||
NotNullOrWhiteSpace(key, nameof(key)); | |||
NotNull(valueFactory, nameof(valueFactory)); | |||
|
|||
return GetOrAddInternal(key, null, (k, r) => valueFactory(k)); | |||
return GetOrAddInternal(key, null, (k, r) => new CacheItem<TCacheValue>(k, valueFactory(k))).Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.Value
will error with NullReference eventually, use ?.Value
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT, there's no way the CacheItem
returned from GetOrAddInternal()
could ever be null? If a function generating null is passed into GetOrAddInternal()
it will explicitly throw inside of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TryGetOrAddInternal
can return null. It is very unlikely, but it can if retries is 0 and get and add fails for example.
Yeah right, in GetOrAddInternal it throws. You are right ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TryGetOrAddInternal
can indeed return null in that case, but GetOrAddInternal
cannot? It will always throw in the described situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, edited my comment => Yeah right, in GetOrAddInternal it throws. You are right ;)
@@ -30,7 +30,26 @@ public TCacheValue GetOrAdd(string key, string region, Func<string, string, TCac | |||
NotNullOrWhiteSpace(region, nameof(region)); | |||
NotNull(valueFactory, nameof(valueFactory)); | |||
|
|||
return GetOrAddInternal(key, region, (k, r) => valueFactory(k, r)); | |||
return GetOrAddInternal(key, region, (k, r) => new CacheItem<TCacheValue>(k, r, valueFactory(k, r))).Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above. :)
{ | ||
value = item.Value; | ||
} | ||
return returnValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
{ | ||
value = item.Value; | ||
} | ||
return returnValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block can be simplified a little
if (TryGetOrAddInternal(
key,
null,
(k, r) =>
{
var newValue = valueFactory(k);
return newValue == null ? null : new CacheItem<TCacheValue>(k, newValue);
},
out CacheItem<TCacheValue> item))
{
value = item.Value;
return true;
}
value = default(TCacheValue);
return false;
Fixes #152.
I have modified tests to cover the new code, but I have not run them, because I don't have redis setup on Windows at the moment.
I duplicated the helper functions used by the other code for the CacheItem version, as I couldn't figure out a way of rewriting the helpers that I was happy with. The obvious way would be to make sure they took a CacheItem and then make the other versions create one on the fly, but that seems a bit wasteful memory-wise especially since it often ends up being unused. Anyway, I'm leaving it up to you. If you want me to rewrite things some other way, by all means ask and I will do so.