Skip to content
Merged
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
4 changes: 3 additions & 1 deletion boxsdk/object/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def generate_events_with_long_polling(self, stream_position=None):
if message == 'new_change':
next_stream_position = stream_position
for event, next_stream_position in self._get_all_events_since(stream_position):
if event_ids.get(event['event_id']) == -1:
try:
event_ids.get(event['event_id'])
except KeyError:
yield event
event_ids.set(event['event_id'])
stream_position = next_stream_position
Expand Down
40 changes: 34 additions & 6 deletions boxsdk/util/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,47 @@

class LRUCache(object):
def __init__(self, capacity=512):
"""
:param capacity:
The Maximum number of key-value pairs can be cached.
:type capacity:
`int`
"""
super(LRUCache, self).__init__()
self.capacity = capacity
self.cache = OrderedDict()

def get(self, key):
try:
value = self.cache.pop(key)
self.cache[key] = value
return value
except KeyError:
return -1
"""
Look up the value in cache using the associated key. Returns the value if found.
Raises :class:`KeyError` otherwise.

:param key:
The key used to look up the cache.
:type key:
`unicode`
:return:
The value associated with the key if exists.
:raises:
:class:`KeyError` if the key doesn't exist.
"""
value = self.cache.pop(key)
self.cache[key] = value
return value

def set(self, key, value=None):
"""
Store the key-value pair to cache.

:param key:
The key associated with the value to be stored. It's used to look up the cache.
:type key:
`unicode`
:param value:
The value to be stored.
:type value:
varies
"""
try:
self.cache.pop(key)
except KeyError:
Expand Down
9 changes: 6 additions & 3 deletions test/unit/util/test_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def keys():
def test_lru_cache_returns_minus_one_for_missing_key(lru_cache, keys):
# pylint:disable=redefined-outer-name
for key in keys:
assert lru_cache.get(key) is -1
with pytest.raises(KeyError):
lru_cache.get(key)


def test_lru_cache_returns_none_for_existing_key(lru_cache, keys):
Expand All @@ -34,9 +35,11 @@ def test_lru_cache_ejects_least_recently_used_key(lru_cache, keys):
for key in keys:
lru_cache.set(key)
lru_cache.set('another key')
assert lru_cache.get(keys[0]) is -1
with pytest.raises(KeyError):
lru_cache.get(keys[0])
assert lru_cache.get('another key') is None
for key in keys[1:]:
assert lru_cache.get(key) is None
lru_cache.set('yet another key')
assert lru_cache.get('another key') is -1
with pytest.raises(KeyError):
lru_cache.get('another key')