Abhilash Raj pushed to branch master at GNU Mailman / Mailman Core

Commits:
af11fa31 by Abhilash Raj at 2018-04-16T02:34:26Z
Allow evicting a single file from the cache.

ICacheManger now allows evicting a single file from the local file cache. This
also changes the behavior of the `evict` method to evict only a single file and
a new `evict_expired` evicts all the expired files, like the `evict` method did 
before.

- - - - -
5e24e802 by Abhilash Raj at 2018-04-16T03:46:24Z
Merge branch 'issue-464' into 'master'

Allow evicting a single file from the local cache.

Closes #464

See merge request mailman/mailman!366
- - - - -


5 changed files:

- src/mailman/docs/NEWS.rst
- src/mailman/interfaces/cache.py
- src/mailman/model/cache.py
- src/mailman/model/tests/test_cache.py
- src/mailman/utilities/protocols.py


Changes:

=====================================
src/mailman/docs/NEWS.rst
=====================================
--- a/src/mailman/docs/NEWS.rst
+++ b/src/mailman/docs/NEWS.rst
@@ -119,6 +119,7 @@ Other
   modify the other generated mapings to use the ``alias_domain``.  The
   ``lmtp`` runner will also accept the alias_domain as an alias for the
   ``email_host``.
+* ``ICacheManager`` now allows evicting a single file from cache.
 
 REST
 ----


=====================================
src/mailman/interfaces/cache.py
=====================================
--- a/src/mailman/interfaces/cache.py
+++ b/src/mailman/interfaces/cache.py
@@ -59,7 +59,14 @@ class ICacheManager(Interface):
         :rtype: bytes or str, depending on the original contents.
         """
 
-    def evict():
+    def evict(file_id):
+        """Evict the file pointed to by file_id.
+
+        :param file_id: The key identifying the contents you want to evict.
+        :type file_id: str
+        """
+
+    def evict_expired():
         """Evict all files which have expired."""
 
     def clear():


=====================================
src/mailman/model/cache.py
=====================================
--- a/src/mailman/model/cache.py
+++ b/src/mailman/model/cache.py
@@ -140,7 +140,18 @@ class CacheManager:
         return contents
 
     @dbconnection
-    def evict(self, store):
+    def evict(self, store, file_id):
+        """See `ICacheManager`"""
+        entry = store.query(CacheEntry).filter(
+            CacheEntry.file_id == file_id).one_or_none()
+        if entry is None:
+            return
+        file_path, dir_path = self._id_to_path(entry.file_id)
+        os.remove(file_path)
+        store.delete(entry)
+
+    @dbconnection
+    def evict_expired(self, store):
         """See `ICacheManager`."""
         # Find all the cache entries which have expired.  We can probably do
         # this more efficiently, but for now there probably aren't that many


=====================================
src/mailman/model/tests/test_cache.py
=====================================
--- a/src/mailman/model/tests/test_cache.py
+++ b/src/mailman/model/tests/test_cache.py
@@ -87,17 +87,26 @@ class TestCache(unittest.TestCase):
         self.assertIsNone(self._cachemgr.get('abc'))
 
     @configuration('mailman', cache_life='1d')
-    def test_evict(self):
+    def test_evict_expired(self):
         # Evicting all expired cache entries makes them inaccessible.
         self._cachemgr.add('abc', 'xyz', lifetime=timedelta(hours=3))
         self._cachemgr.add('def', 'uvw', lifetime=timedelta(days=3))
         self.assertEqual(self._cachemgr.get('abc'), 'xyz')
         self.assertEqual(self._cachemgr.get('def'), 'uvw')
         factory.fast_forward(days=1)
-        self._cachemgr.evict()
+        self._cachemgr.evict_expired()
         self.assertIsNone(self._cachemgr.get('abc'))
         self.assertEqual(self._cachemgr.get('def'), 'uvw')
 
+    def test_evict(self):
+        # Evicting a single cached file makes them inaccessible.
+        file_id = self._cachemgr.add('abc', 'xyz', lifetime=timedelta(hours=2))
+        self.assertEqual(self._cachemgr.get('abc'), 'xyz')
+        self._cachemgr.evict(file_id)
+        self.assertIsNone(self._cachemgr.get('abc'))
+        # Nothing happens if we try to evict a non-existent cache entry.
+        self.assertIsNone(self._cachemgr.evict('somenonexistentid'))
+
     def test_clear(self):
         # Clearing the cache gets rid of all entries, regardless of lifetime.
         self._cachemgr.add('abc', 'xyz', lifetime=timedelta(hours=3))


=====================================
src/mailman/utilities/protocols.py
=====================================
--- a/src/mailman/utilities/protocols.py
+++ b/src/mailman/utilities/protocols.py
@@ -28,13 +28,14 @@ from urllib.parse import urlparse
 from zope.component import getUtility
 
 COMMASPACE = ', '
+REQUEST_TIMEOUT = 5
 
 
 @public
 def get(url, **kws):
     parsed = urlparse(url)
     if parsed.scheme in ('http', 'https'):
-        response = requests.get(url, **kws)
+        response = requests.get(url, timeout=REQUEST_TIMEOUT, **kws)
         response.raise_for_status()
         return response.text
     if parsed.scheme == 'file':



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/5f0ffb6d55d3458d269892b3f42170a65dc89426...5e24e802200aef5d2470983a5d7b95ab65a33841

---
View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/5f0ffb6d55d3458d269892b3f42170a65dc89426...5e24e802200aef5d2470983a5d7b95ab65a33841
You're receiving this email because of your account on gitlab.com.
_______________________________________________
Mailman-checkins mailing list
Mailman-checkins@python.org
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to