Skip to content
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

feature - adding ability to set the amount of bytes cached #38

Merged
merged 1 commit into from Aug 25, 2014

Conversation

sinemetu1
Copy link
Contributor

Hi @ionrock,

I'm working on a feature for http://github.com/mozilla/mozregression in which we could set the maximum bytes that we cache using cachecontrol. This PR adds this capability to the FileCache class with a max_bytes param. The default is 1Gb.

The implementation in this PR is using the sys.getsizeof(value) to take care of the byte tracking. Another approach might be to do something with os.path.getsize() like this against the top cache dir.

Let me know what you think! :)

@ionrock
Copy link
Contributor

ionrock commented Aug 25, 2014

I'm going to go ahead and merge this, but in the future, I'd like to use a mixin for this sort of functionality as this could be valuable in other scenarios. Specifically, the default memory cache could use a simple way to set the max size. I already have plans to create a LRU mixin that allows a max size and this effectively seems similar.

@sinemetu1 Let me know it the LRU mentioned above and in #36 if implemented as a mixin would be sufficient. I might extract the functionality before a release so as to avoid overloading the FileCache with too many concerns. In theory, a FileCache is relatively safe to use unbounded b/c disk space is cheap. Limiting the amount of space is something that is slightly more specialized, which I'd like to code as a mixin if possible.

Thanks for the PR!

ionrock added a commit that referenced this pull request Aug 25, 2014
feature - adding ability to set the amount of bytes cached
@ionrock ionrock merged commit cf58413 into psf:master Aug 25, 2014
ionrock added a commit that referenced this pull request Sep 10, 2014
… been read before caching.

I copied over the is_fp_closed from urllib3 and reversed the ordering of
the try / except in order to give preference to HTTPResponses before
real file pointers.
ionrock added a commit that referenced this pull request Sep 10, 2014
After a review with @dstufft the PR wasn't as robust as I'd hoped. The
use of warnings (which is a good idea!) should really be done via
logging so it can be easily turned on/off. Also, @dstufft mentioned
sys.getsizeof is not entirely reliable across all systems. Most
importantly though, the cache assumes it is the only instance. On a
system restart a new instance's size goes to 0 even though there might
be cached items.
@ionrock
Copy link
Contributor

ionrock commented Sep 10, 2014

After a code review with @dstufft I've rolled this back. The biggest reason is that the size of the cache will revert to 0 when the process restarts. This kind of defeats the purpose! Also, os.getsizeof is not the most dependable means of getting the size across different OSs. Finally, using warning (which is a good idea) ends up being noisier than using log messages.

I think a better tactic would be inherit or make a mixin class based on the FileCache and use that directly in your app. By doing so you avoid generalizaton for different operating systems and you can integrate whatever notification system you need. Also, if resetting the cache to 0 on start doesn't cause a problem, then that is great!

Sorry about reverting this change and thanks for submitting the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants