-
Notifications
You must be signed in to change notification settings - Fork 22
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
No cache option for PyCrest __init__ and get() calls #47
Conversation
…allow a "no cache" configuration. Also set DummyCache as default cache (cache=None). Change existing tests to match this new behavior, and add test for DummyCache
This looks great! |
This isn't really your changes, but I think that whole cache vs cache_dir thing is a mess. You can pass in via the cache argument an instance of a class that derives from APICache, and it will use that as a cache. Or you can pass in the name of a class which may or may not derive from APICache and it will instantiate an object of this type and use it as a cache. Or you can pass in a directory via cache_dir and it will instantiate a FileCache with that directory. But what if you pass in both a cache and a cache_dir? What should it do? This pull changes the priority of cache_dir and cache unnecessarily, which possibly breaks existing dodgy code. But noone should be passing in both objects so it doesn't really matter. However I think the entire interface would be much neater if we got rid of the historic cache_dir argument (or maybe deprecated it and printed an annoying warning), because there shouldn't be two different ways of specifying what cache you want. I'm not sure what people think about the ability to pass in the type of a class rather than a class object? But seeing as you can't specify any arguments when you do this, and the current implementation doesn't even guarantee that you end up with an object that has get/put/invalidate methods on it, I think it needs to be changed. I think there should be a single argument (cache) which accepts an instance of an object that inherits from APICache. If we retain the ability to pass in the type of an object, then we would need a check that ensures the object we instantiate actually inherits from APICache. But I'm not sure of the benefit of being able to pass in the type. This probably needs a new issue to deal with though cos its only a little bit about this pull request and more about the interface in general. |
Yep, i totally agree with that. While I did these changes I actually though about it (cache/cache_dir and the cache type to instanciate). I had to change the order (cache_dir vs cache) in the if statement as I changed the default value of "cache" (as it's now a DictCache() by default, even with cache_dir, it was always a DictCache). I believe the best things to do are:
I know it may break compatibility to older versions, but it wasn't the best design, as in some case we had non trivial choice being mad arbitrary with the current code. Quickly, something like that: if cache and isinstance(cache, APICache):
# use the given cache, as we are sure it's OK
self.cache = cache
elif cache is None:
# Dummy if no cache
self.cache = DummyCache()
else:
# if we are here, it's because the user gave a cache (he wants some cache)
# but it was not the correct type.
raise ValueError('Given cache must implement APICache') Why an exception ? Because I believe we shouldn't use a "random cache" when the user wanted to use one for his usecase, so we have to tell him he did it wrong, thus I think returning an exception is the best case. |
that logic looks reasonable to me |
@hkraal if you agree with this logic (and if you want) I can add this to my PR, so you can add it both. If not, i'll make an issue of this, not to lose tracks and as a reminder |
@Kyria Sorry, somehow I missed the mention. Please create an issue to address the current cache implementation in the future, for now this works which is good enough. From my point of view it should be a nice feature update for v2.x or something |
No cache option for PyCrest __init__ and get() calls (pycrest#47)
As i stated in #44, there were no solutions to prevent pycrest to do some caching, even by stating
cache=None
This PR adds this features.
More precisely, for cache definition in EVE class:
cache=None
you will use theDummyCache
which does nothing but expose expected caching methods (get()
,put()
,invalidate()
) not to break the existing code withif cache==None everywhere
Concerning the get() calls:
caching=False
orcaching=None
, it will only not store the result in the cache, but you will still use the existing cache, if available.The doc is updated with examples.