[tor-commits] [nyx/master] Move cache back into base module
atagar at torproject.org
atagar at torproject.org
Sun Sep 3 01:24:13 UTC 2017
commit 6678e5021dd626afbccbf9aebb15819d40deae3f
Author: Damian Johnson <atagar at torproject.org>
Date: Thu Aug 31 12:07:28 2017 -0700
Move cache back into base module
Well, that didn't last very long. Got annoyed by calling 'nyx.cache.cache()'.
Oh well.
---
nyx/__init__.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
nyx/cache.py | 83 ---------------------------------------------------------
test/cache.py | 12 ++++-----
3 files changed, 78 insertions(+), 89 deletions(-)
diff --git a/nyx/__init__.py b/nyx/__init__.py
index c2032bc..3c3c939 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -33,6 +33,7 @@ Tor curses monitoring application.
import distutils.spawn
import os
+import sqlite3
import sys
import threading
import time
@@ -84,6 +85,7 @@ CONFIG = stem.util.conf.config_dict('nyx', {
NYX_INTERFACE = None
TOR_CONTROLLER = None
+CACHE = None
BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-1])
# technically can change but we use this query a *lot* so needs to be cached
@@ -94,6 +96,15 @@ stem.control.CACHEABLE_GETINFO_PARAMS = list(stem.control.CACHEABLE_GETINFO_PARA
stem.control.LOG_CACHE_FETCHES = False
+SCHEMA_VERSION = 1 # version of our scheme, bump this if you change the following
+SCHEMA = (
+ 'CREATE TABLE schema(version NUMBER)',
+ 'INSERT INTO schema(version) VALUES (%i)' % SCHEMA_VERSION,
+
+ 'CREATE TABLE relays(fingerprint TEXT PRIMARY KEY, address TEXT, or_port NUMBER, nickname TEXT)',
+)
+
+
try:
uses_settings = stem.util.conf.uses_settings('nyx', os.path.join(BASE_DIR, 'settings'), lazy_load = False)
except IOError as exc:
@@ -189,6 +200,21 @@ def tor_controller():
return TOR_CONTROLLER
+def cache():
+ """
+ Provides the sqlite cache for application data.
+
+ :returns: :class:`~nyx.cache.Cache` for our applicaion
+ """
+
+ global CACHE
+
+ if CACHE is None:
+ CACHE = Cache()
+
+ return CACHE
+
+
def show_message(message = None, *attr, **kwargs):
"""
Shows a message in our header.
@@ -311,6 +337,52 @@ def join(entries, joiner = ' ', size = None):
return result
+class Cache(object):
+ """
+ Cache for frequently used information.
+ """
+
+ def __init__(self):
+ self._conn_lock = threading.RLock()
+ cache_path = nyx.data_directory('cache.sqlite')
+
+ if cache_path:
+ try:
+ self._conn = sqlite3.connect(cache_path)
+ schema = self._conn.execute('SELECT version FROM schema').fetchone()[0]
+ except:
+ schema = None
+
+ if schema == SCHEMA_VERSION:
+ stem.util.log.info('Cache loaded from %s' % cache_path)
+ else:
+ if schema is None:
+ stem.util.log.info('Cache at %s is missing a schema, clearing it.' % cache_path)
+ else:
+ stem.util.log.info('Cache at %s has schema version %s but the current version is %s, clearing it.' % (cache_path, schema, SCHEMA_VERSION))
+
+ self._conn.close()
+ os.remove(cache_path)
+ self._conn = sqlite3.connect(cache_path)
+
+ for cmd in SCHEMA:
+ self._conn.execute(cmd)
+ else:
+ stem.util.log.info('Unable to cache to disk. Using an in-memory cache instead.')
+ self._conn = sqlite3.connect(':memory:')
+
+ for cmd in SCHEMA:
+ self._conn.execute(cmd)
+
+ def query(self, query, *param):
+ """
+ Performs a query on our cache.
+ """
+
+ with self._conn_lock:
+ return self._conn.execute(query, param)
+
+
class Interface(object):
"""
Overall state of the nyx interface.
diff --git a/nyx/cache.py b/nyx/cache.py
deleted file mode 100644
index f4c6f3f..0000000
--- a/nyx/cache.py
+++ /dev/null
@@ -1,83 +0,0 @@
-"""
-Cache for frequently needed information. This persists to disk if we can, and
-otherwise is an in-memory cache.
-"""
-
-import os
-import sqlite3
-import threading
-
-import stem.util.log
-
-import nyx
-
-CACHE = None
-
-SCHEMA_VERSION = 1 # version of our scheme, bump this if you change the following
-SCHEMA = (
- 'CREATE TABLE schema(version NUMBER)',
- 'INSERT INTO schema(version) VALUES (%i)' % SCHEMA_VERSION,
-
- 'CREATE TABLE relays(fingerprint TEXT PRIMARY KEY, address TEXT, or_port NUMBER, nickname TEXT)',
-)
-
-
-def cache():
- """
- Provides the sqlite cache for application data.
-
- :returns: :class:`~nyx.cache.Cache` for our applicaion
- """
-
- global CACHE
-
- if CACHE is None:
- CACHE = Cache()
-
- return CACHE
-
-
-class Cache(object):
- """
- Cache for frequently used information.
- """
-
- def __init__(self):
- self._conn_lock = threading.RLock()
- cache_path = nyx.data_directory('cache.sqlite')
-
- if cache_path:
- try:
- self._conn = sqlite3.connect(cache_path)
- schema = self._conn.execute('SELECT version FROM schema').fetchone()[0]
- except:
- schema = None
-
- if schema == SCHEMA_VERSION:
- stem.util.log.info('Cache loaded from %s' % cache_path)
- else:
- if schema is None:
- stem.util.log.info('Cache at %s is missing a schema, clearing it.' % cache_path)
- else:
- stem.util.log.info('Cache at %s has schema version %s but the current version is %s, clearing it.' % (cache_path, schema, SCHEMA_VERSION))
-
- self._conn.close()
- os.remove(cache_path)
- self._conn = sqlite3.connect(cache_path)
-
- for cmd in SCHEMA:
- self._conn.execute(cmd)
- else:
- stem.util.log.info('Unable to cache to disk. Using an in-memory cache instead.')
- self._conn = sqlite3.connect(':memory:')
-
- for cmd in SCHEMA:
- self._conn.execute(cmd)
-
- def query(self, query, *param):
- """
- Performs a query on our cache.
- """
-
- with self._conn_lock:
- return self._conn.execute(query, param)
diff --git a/test/cache.py b/test/cache.py
index f26ede9..91e9ab8 100644
--- a/test/cache.py
+++ b/test/cache.py
@@ -5,7 +5,7 @@ Unit tests for nyx.cache.
import tempfile
import unittest
-import nyx.cache
+import nyx
from mock import Mock, patch
@@ -17,7 +17,7 @@ NICKNAME = 'caersidi'
class TestCache(unittest.TestCase):
def setUp(self):
- nyx.cache.CACHE = None # drop cached database reference
+ nyx.CACHE = None # drop cached database reference
@patch('nyx.data_directory', Mock(return_value = None))
def test_memory_cache(self):
@@ -25,7 +25,7 @@ class TestCache(unittest.TestCase):
Create a cache in memory.
"""
- cache = nyx.cache.cache()
+ cache = nyx.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)
@@ -38,13 +38,13 @@ class TestCache(unittest.TestCase):
with tempfile.NamedTemporaryFile(suffix = '.sqlite') as tmp:
with patch('nyx.data_directory', Mock(return_value = tmp.name)):
- cache = nyx.cache.cache()
+ cache = nyx.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
+ nyx.CACHE = None
- cache = nyx.cache.cache()
+ cache = nyx.cache()
self.assertEqual(NICKNAME, cache.query('SELECT nickname FROM relays WHERE fingerprint=?', FINGERPRINT).fetchone()[0])
More information about the tor-commits
mailing list