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

Move #expires, #expired, and #ttl to ExpiringObject class. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions reppy/cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .policy import DefaultObjectPolicy, ReraiseExceptionPolicy
from ..robots import Robots, AllowNone, Agent
from ..ttl import HeaderWithDefaultPolicy


class ExpiringObject(object):
Expand All @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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]
Copy link

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?

Copy link
Contributor Author

@b4hand b4hand Jun 6, 2018

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.


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)
Copy link

Choose a reason for hiding this comment

The 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 Robots that returns a pair of (response, obj), but I think this is fine as is.

return (expires[0], robots)


class AgentCache(BaseCache):
Expand All @@ -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))
Loading