-
Notifications
You must be signed in to change notification settings - Fork 41
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
Move #expires, #expired, and #ttl to ExpiringObject class. #89
Open
b4hand
wants to merge
1
commit into
master
Choose a base branch
from
cbf/refactor-expires
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+973
−1,516
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
from .policy import DefaultObjectPolicy, ReraiseExceptionPolicy | ||
from ..robots import Robots, AllowNone, Agent | ||
from ..ttl import HeaderWithDefaultPolicy | ||
|
||
|
||
class ExpiringObject(object): | ||
|
@@ -17,14 +18,14 @@ def __init__(self, factory): | |
self.factory = factory | ||
self.lock = threading.Lock() | ||
self.obj = None | ||
self.expires = 0 | ||
self._expires = 0 | ||
self.exception = None | ||
|
||
def get(self): | ||
'''Get the wrapped object.''' | ||
if (self.obj is None) or (time.time() >= self.expires): | ||
if self.obj is None or self.expired: | ||
with self.lock: | ||
self.expires, self.obj = self.factory() | ||
self._expires, self.obj = self.factory() | ||
if isinstance(self.obj, BaseException): | ||
self.exception = self.obj | ||
else: | ||
|
@@ -35,18 +36,36 @@ def get(self): | |
else: | ||
return self.obj | ||
|
||
@property | ||
def expired(self): | ||
'''True if the current time is past its expiration.''' | ||
return time.time() > self.expires | ||
|
||
@property | ||
def expires(self): | ||
'''The expiration of this robots.txt.''' | ||
return self._expires | ||
|
||
@property | ||
def ttl(self): | ||
'''Remaining time for this response to be considered valid.''' | ||
return max(self.expires - time.time(), 0) | ||
|
||
|
||
class BaseCache(object): | ||
'''A base cache class.''' | ||
|
||
DEFAULT_CACHE_POLICY = ReraiseExceptionPolicy(ttl=600) | ||
DEFAULT_TTL_POLICY = Robots.DEFAULT_TTL_POLICY | ||
# The default TTL policy is to cache for 3600 seconds or what's | ||
# provided in the headers, and a minimum of 600 seconds | ||
DEFAULT_TTL_POLICY = HeaderWithDefaultPolicy(default=3600, minimum=600) | ||
|
||
def __init__(self, capacity, cache_policy=None, ttl_policy=None, *args, **kwargs): | ||
self.cache_policy = cache_policy or self.DEFAULT_CACHE_POLICY | ||
self.ttl_policy = ttl_policy or self.DEFAULT_TTL_POLICY | ||
self.cache = LRUCache(maxsize=capacity, missing=self.missing) | ||
self.args = args | ||
self.after_response_hook = kwargs.pop('after_response_hook', None) | ||
self.kwargs = kwargs | ||
|
||
def get(self, url): | ||
|
@@ -83,9 +102,17 @@ def allowed(self, url, agent): | |
|
||
def fetch(self, url): | ||
'''Return (expiration, Robots) for the robots.txt at the provided URL.''' | ||
expires = [0] | ||
|
||
def check_ttl_hook(response): | ||
if not isinstance(response, Exception): | ||
expires[0] = self.ttl_policy.expires(response) | ||
if self.after_response_hook is not None: | ||
self.after_response_hook(response) | ||
|
||
robots = Robots.fetch( | ||
url, ttl_policy=self.ttl_policy, *self.args, **self.kwargs) | ||
return (robots.expires, robots) | ||
url, after_response_hook=check_ttl_hook, *self.args, **self.kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that this doesn't feel great, but I don't see any reason not to trust it. We could also have a method off of |
||
return (expires[0], robots) | ||
|
||
|
||
class AgentCache(BaseCache): | ||
|
@@ -104,6 +131,14 @@ def allowed(self, url): | |
|
||
def fetch(self, url): | ||
'''Return (expiration, Agent) for the robots.txt at the provided URL.''' | ||
expires = [0] | ||
|
||
def check_ttl_hook(response): | ||
if not isinstance(response, Exception): | ||
expires[0] = self.ttl_policy.expires(response) | ||
if self.after_response_hook is not None: | ||
self.after_response_hook(response) | ||
|
||
robots = Robots.fetch( | ||
url, ttl_policy=self.ttl_policy, *self.args, **self.kwargs) | ||
return (robots.expires, robots.agent(self.agent)) | ||
url, after_response_hook=check_ttl_hook, *self.args, **self.kwargs) | ||
return (expires[0], robots.agent(self.agent)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's been a while since I've done python. Is this part of that weird closure thing? As in, it has to be an array for us to be able to save a value in this scope from
check_ttl_hook
?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.
Yes, that's correct. We need a mutable object to modify in the closure. A bare assignment will create a new local variable rather than modify the variable in the outer scope.