Skip to content

Commit

Permalink
Fix #256: Deprecate MRUCache class.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jul 15, 2024
1 parent efd1d3b commit a1f0cea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ computed when the item is inserted into the cache.
This class discards the most recently used items first to make
space when necessary.

.. deprecated:: 5.4

`MRUCache` has been deprecated due to lack of use, to reduce
maintenance. Please choose another cache implementation that suits
your needs.

.. autoclass:: RRCache(maxsize, choice=random.choice, getsizeof=None)
:members: choice, popitem

Expand Down
4 changes: 4 additions & 0 deletions src/cachetools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ class MRUCache(Cache):
"""Most Recently Used (MRU) cache implementation."""

def __init__(self, maxsize, getsizeof=None):
from warnings import warn

warn("MRUCache is deprecated", DeprecationWarning, stacklevel=2)

Cache.__init__(self, maxsize, getsizeof)
self.__order = collections.OrderedDict()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def decorator(self, maxsize, **kwargs):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
d = cachetools.func.mru_cache(maxsize, **kwargs)
self.assertEqual(len(w), 1)
self.assertNotEqual(len(w), 0)
self.assertIs(w[0].category, DeprecationWarning)
return d

Expand Down
21 changes: 18 additions & 3 deletions tests/test_mru.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import warnings

from cachetools import MRUCache

Expand All @@ -7,10 +8,16 @@

class MRUCacheTest(unittest.TestCase, CacheTestMixin):

# TODO: method to create cache that can be overridden
Cache = MRUCache

def test_evict__writes_only(self):
cache = MRUCache(maxsize=2)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
cache = MRUCache(maxsize=2)
self.assertEqual(len(w), 1)
self.assertIs(w[0].category, DeprecationWarning)

cache[1] = 1
cache[2] = 2
Expand All @@ -22,7 +29,11 @@ def test_evict__writes_only(self):
assert 3 in cache

def test_evict__with_access(self):
cache = MRUCache(maxsize=2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
cache = MRUCache(maxsize=2)
self.assertEqual(len(w), 1)
self.assertIs(w[0].category, DeprecationWarning)

cache[1] = 1
cache[2] = 2
Expand All @@ -34,7 +45,11 @@ def test_evict__with_access(self):
assert 3 in cache

def test_evict__with_delete(self):
cache = MRUCache(maxsize=2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
cache = MRUCache(maxsize=2)
self.assertEqual(len(w), 1)
self.assertIs(w[0].category, DeprecationWarning)

cache[1] = 1
cache[2] = 2
Expand Down

0 comments on commit a1f0cea

Please sign in to comment.