-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Unified storage interfaces #1184
Conversation
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.
Love it! great stuff.
51abd9a
to
153d097
Compare
One thing this still needs for the memory and file backends is a way to (semi) automatically evict expired items. Especially for the in-memory storage when used as a cache this could become a problem otherwise, since items would only be evicted if you're trying to access them. There are two general ways to go about this:
For the second option, there are more things to consider: When should eviction be performed?
How should items be evicted?
Both solutions 1 and 2 can cause performance issues, depending on the size of the namespace. For smaller ones, checking every item would be more performant, while for larger ones, random sampling would be. We could of course also make periodic eviction opt-in, and let the user decide how they want to handle it, based on their use case. Another option would be to simply file this under "If you need this functionality, use a redis backend". |
Using collections.deque and setting a maxlen (or have the user specify one) would be a relatively cheap way of ensuring the cache doesn't grow outside the bounds the user requested. Also, the additional functionality you describe is best served by redis imo. It's purpose built for this very task 😄 |
This isn't just a cache. These storage backends are also used for sessions, for which this would be very undesirable behavior.
I kinda agree, that's why I mentioned it as an option. On the other hand, especially for use as a cache, in-memory storage that's not reliant on another service is very convenient, and for many use cases good enough. |
How about an after response hook? Given there is no one-size-fits-all solution to clearing expired cache objects, and that we'd only really want to encourage using in-memory storage when the user knows what they are getting themselves into, I think we should document that the user has to take on that responsibility themselves, and perhaps offer some example solutions as you've listed above. |
8958d9c
to
d7c3c39
Compare
b924864
to
63f1559
Compare
Okay, this is mostly done now and only missing docs. One additional thing we might want to do is to add even more unification by getting rid of the
@starlite-api/maintainers wdyt? |
63f1559
to
aeb5c2b
Compare
I think the cache abstraction makes it easy to work with the cache and offers a uniform interface. It's meant to be user facing and simple to operate, as a facade. Do you think your proposal will offer the same kind of DX? As for storage instead if cache, i think semantics are arguable here and |
I think it would, since it removes one layer of abstraction that doesn't do much anyway. With this PR, I'd argue it would actually be a better DX to have things more integrated, but I guess that's a general question whether or not we want to go that direction. I'm not yet sure what the best API for this is though, maybe something like the
I'm impartial to naming it either way. |
The important thing is that the method names and signatures are standardized by the abstraction. If this is the case in your refactor and you always have the same methods / signatures etc, you can remove the cache abstraction. |
It is! That's one of the main reasons for this refactor. There is one additional method on the file and memory storage, but they all implement the |
Okay, I want to take the steps towards removing the |
671f0aa
to
9a0158b
Compare
be4a410
to
474012b
Compare
Kudos, SonarCloud Quality Gate passed! |
Create a new
storage
module providing abstraction for storing data in:These new backends are used by the cache as well as session backends, reducing code and test duplication.
Some more simplifications were made in the process:
Cache
class and shifted into the storage backends where neededThe SQLAlchemy session backend was removed since it was adding unnecessary complexity for a use case we don't want to encourage anyway.
TODO
PR Checklist
CONTRIBUTING.rst
?