[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [nyx/master] Drop cache context manager



commit 068ace653d9b3127cdef1c2b9ddd33493af957dc
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Aug 31 11:58:27 2017 -0700

    Drop cache context manager
    
    Now that we have a class that can do its own locking there's no need to require
    a context manager.
---
 nyx/cache.py  | 14 ++++++--------
 test/cache.py | 22 ++++++++++++----------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/nyx/cache.py b/nyx/cache.py
index 8892be3..f4c6f3f 100644
--- a/nyx/cache.py
+++ b/nyx/cache.py
@@ -3,7 +3,6 @@ Cache for frequently needed information. This persists to disk if we can, and
 otherwise is an in-memory cache.
 """
 
-import contextlib
 import os
 import sqlite3
 import threading
@@ -13,7 +12,6 @@ import stem.util.log
 import nyx
 
 CACHE = None
-CACHE_LOCK = threading.RLock()
 
 SCHEMA_VERSION = 1  # version of our scheme, bump this if you change the following
 SCHEMA = (
@@ -24,7 +22,6 @@ SCHEMA = (
 )
 
 
-@contextlib.contextmanager
 def cache():
   """
   Provides the sqlite cache for application data.
@@ -34,11 +31,10 @@ def cache():
 
   global CACHE
 
-  with CACHE_LOCK:
-    if CACHE is None:
-      CACHE = Cache()
+  if CACHE is None:
+    CACHE = Cache()
 
-    yield CACHE
+  return CACHE
 
 
 class Cache(object):
@@ -47,6 +43,7 @@ class Cache(object):
   """
 
   def __init__(self):
+    self._conn_lock = threading.RLock()
     cache_path = nyx.data_directory('cache.sqlite')
 
     if cache_path:
@@ -82,4 +79,5 @@ class Cache(object):
     Performs a query on our cache.
     """
 
-    return self._conn.execute(query, param)
+    with self._conn_lock:
+      return self._conn.execute(query, param)
diff --git a/test/cache.py b/test/cache.py
index 733fbca..f26ede9 100644
--- a/test/cache.py
+++ b/test/cache.py
@@ -25,10 +25,11 @@ class TestCache(unittest.TestCase):
     Create a cache in memory.
     """
 
-    with nyx.cache.cache() as cache:
-      self.assertEqual((0, 'main', ''), cache.query('PRAGMA database_list').fetchone())
-      cache.query('INSERT INTO relays(fingerprint, address, or_port, nickname) VALUES (?,?,?,?)', FINGERPRINT, ADDRESS, PORT, NICKNAME)
-      self.assertEqual(NICKNAME, cache.query('SELECT nickname FROM relays WHERE fingerprint=?', FINGERPRINT).fetchone()[0])
+    cache = nyx.cache.cache()
+
+    self.assertEqual((0, 'main', ''), cache.query('PRAGMA database_list').fetchone())
+    cache.query('INSERT INTO relays(fingerprint, address, or_port, nickname) VALUES (?,?,?,?)', FINGERPRINT, ADDRESS, PORT, NICKNAME)
+    self.assertEqual(NICKNAME, cache.query('SELECT nickname FROM relays WHERE fingerprint=?', FINGERPRINT).fetchone()[0])
 
   def test_file_cache(self):
     """
@@ -37,12 +38,13 @@ class TestCache(unittest.TestCase):
 
     with tempfile.NamedTemporaryFile(suffix = '.sqlite') as tmp:
       with patch('nyx.data_directory', Mock(return_value = tmp.name)):
-        with nyx.cache.cache() as cache:
-          self.assertEqual((0, 'main', tmp.name), cache.query('PRAGMA database_list').fetchone())
-          cache.query('INSERT INTO relays(fingerprint, address, or_port, nickname) VALUES (?,?,?,?)', FINGERPRINT, ADDRESS, PORT, NICKNAME)
-          cache._conn.commit()
+        cache = nyx.cache.cache()
+
+        self.assertEqual((0, 'main', tmp.name), cache.query('PRAGMA database_list').fetchone())
+        cache.query('INSERT INTO relays(fingerprint, address, or_port, nickname) VALUES (?,?,?,?)', FINGERPRINT, ADDRESS, PORT, NICKNAME)
+        cache._conn.commit()
 
         nyx.cache.CACHE = None
 
-        with nyx.cache.cache() as cache:
-          self.assertEqual(NICKNAME, cache.query('SELECT nickname FROM relays WHERE fingerprint=?', FINGERPRINT).fetchone()[0])
+        cache = nyx.cache.cache()
+        self.assertEqual(NICKNAME, cache.query('SELECT nickname FROM relays WHERE fingerprint=?', FINGERPRINT).fetchone()[0])



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits