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

ENH: Add expiring cache. #1130

Merged
merged 2 commits into from Apr 15, 2016

Conversation

Projects
None yet
3 participants
@ehebert
Member

ehebert commented Apr 14, 2016

Add a cache interface which supports expirable entries with a changeable
backend for the cache into which they are entered.

The default cache is a dict but could swapped for
cachetools.LRUCache or any other cache which supports __get__,
__set__, and __del__.

So that consumers can change the use of CachedObjects stored in a
cache from:

self._cache = {}

...

try:
    obj = self._cache[key]
    try:
        return obj.unwrap(dt)
    except Expired:
        pass
except KeyError:
    pass

...

self._cache[key] = CachedObject(value, new_expiration)

to:

self._cache = ExpiringCache(LRUCache(maxsize=6))

...

try:
    return self._cache.get(key, dt)
except KeyError:
    # Get fresh value
    ...

    self._cache.set(key, value, new_expiration)
with self.assertRaises(KeyError) as e:
self.assertEqual(cache.get('foo', after))
self.assertEqual(e.exception.args, ('foo',))

This comment has been minimized.

@ssanderson

ssanderson Apr 14, 2016

Member

This line should be outside the with block.

# Should raise same KeyError after deletion.
with self.assertRaises(KeyError) as e:
self.assertEqual(cache.get('foo', after))

This comment has been minimized.

@ssanderson

ssanderson Apr 14, 2016

Member

Should this be expires and/or before?

This comment has been minimized.

@ehebert

ehebert Apr 14, 2016

Member

Will fix.

self.assertEqual(e.exception.args, ('foo',))
# Second value should still exist.
self.assertEqual(cache.get('bar', after), 2)

This comment has been minimized.

@ssanderson

ssanderson Apr 14, 2016

Member

This reads a little misleadingly. I'd just give bar a different expiration entirely.

This comment has been minimized.

@ehebert

ehebert Apr 14, 2016

Member

Good point, will do.

@ssanderson

This comment has been minimized.

Member

ssanderson commented Apr 14, 2016

Minor test notes, otherwise LGTM.

@ehebert ehebert force-pushed the expiring-cache branch from ebd121b to 607ac2d Apr 14, 2016

@ehebert

This comment has been minimized.

Member

ehebert commented Apr 14, 2016

Squashed in the test notes.

@ssanderson

This comment has been minimized.

Member

ssanderson commented Apr 14, 2016

Might be worth throwing in a whatsnew entry.

ehebert added some commits Apr 14, 2016

ENH: Add expiring cache.
Add a cache interface which supports expirable entries with a changeable
backend for the cache into which they are entered.

The default cache is a `dict` but could swapped for
`cachetools.LRUCache` or any other cache which supports `__get__`,
`__set__`, and `__del__`.

So that consumers can change the use of `CachedObjects` stored in a
cache from:

```
self._cache = {}

...

try:
    obj = self._cache[key]
    try:
        return obj.unwrap(dt)
    except Expired:
        pass
except KeyError:
    pass

...

self._cache[key] = CachedObject(value, new_expiration)
```

to:

```
self._cache = ExpiringCache(LRUCache(maxsize=6))

...

try:
    return self._cache.get(key, dt)
except KeyError:
    # Get fresh value
    ...

    self._cache.set(key, value, new_expiration)
```

@ehebert ehebert force-pushed the expiring-cache branch from e8a41b6 to ee26b57 Apr 14, 2016

@coveralls

This comment has been minimized.

coveralls commented Apr 14, 2016

Coverage Status

Coverage increased (+0.009%) to 84.27% when pulling ee26b57 on expiring-cache into 8666768 on master.

@ehebert ehebert merged commit 9e2c5d9 into master Apr 15, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ehebert ehebert deleted the expiring-cache branch Apr 15, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment