Skip to content

Commit 056042a

Browse files
committed
Merge pull request #4 from box/lru_cache
Improved the LRUCache class and added dostrings
2 parents cfe1acb + 2fffb54 commit 056042a

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

boxsdk/object/events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def generate_events_with_long_polling(self, stream_position=None):
130130
if message == 'new_change':
131131
next_stream_position = stream_position
132132
for event, next_stream_position in self._get_all_events_since(stream_position):
133-
if event_ids.get(event['event_id']) == -1:
133+
try:
134+
event_ids.get(event['event_id'])
135+
except KeyError:
134136
yield event
135137
event_ids.set(event['event_id'])
136138
stream_position = next_stream_position

boxsdk/util/lru_cache.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,47 @@
66

77
class LRUCache(object):
88
def __init__(self, capacity=512):
9+
"""
10+
:param capacity:
11+
The Maximum number of key-value pairs can be cached.
12+
:type capacity:
13+
`int`
14+
"""
915
super(LRUCache, self).__init__()
1016
self.capacity = capacity
1117
self.cache = OrderedDict()
1218

1319
def get(self, key):
14-
try:
15-
value = self.cache.pop(key)
16-
self.cache[key] = value
17-
return value
18-
except KeyError:
19-
return -1
20+
"""
21+
Look up the value in cache using the associated key. Returns the value if found.
22+
Raises :class:`KeyError` otherwise.
23+
24+
:param key:
25+
The key used to look up the cache.
26+
:type key:
27+
`unicode`
28+
:return:
29+
The value associated with the key if exists.
30+
:raises:
31+
:class:`KeyError` if the key doesn't exist.
32+
"""
33+
value = self.cache.pop(key)
34+
self.cache[key] = value
35+
return value
2036

2137
def set(self, key, value=None):
38+
"""
39+
Store the key-value pair to cache.
40+
41+
:param key:
42+
The key associated with the value to be stored. It's used to look up the cache.
43+
:type key:
44+
`unicode`
45+
:param value:
46+
The value to be stored.
47+
:type value:
48+
varies
49+
"""
2250
try:
2351
self.cache.pop(key)
2452
except KeyError:

test/unit/util/test_lru_cache.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def keys():
1818
def test_lru_cache_returns_minus_one_for_missing_key(lru_cache, keys):
1919
# pylint:disable=redefined-outer-name
2020
for key in keys:
21-
assert lru_cache.get(key) is -1
21+
with pytest.raises(KeyError):
22+
lru_cache.get(key)
2223

2324

2425
def test_lru_cache_returns_none_for_existing_key(lru_cache, keys):
@@ -34,9 +35,11 @@ def test_lru_cache_ejects_least_recently_used_key(lru_cache, keys):
3435
for key in keys:
3536
lru_cache.set(key)
3637
lru_cache.set('another key')
37-
assert lru_cache.get(keys[0]) is -1
38+
with pytest.raises(KeyError):
39+
lru_cache.get(keys[0])
3840
assert lru_cache.get('another key') is None
3941
for key in keys[1:]:
4042
assert lru_cache.get(key) is None
4143
lru_cache.set('yet another key')
42-
assert lru_cache.get('another key') is -1
44+
with pytest.raises(KeyError):
45+
lru_cache.get('another key')

0 commit comments

Comments
 (0)