Repository: vertexproject/synapse

Issue:
dynamically disabling caching on a cortex seems broken
Trigger with setConfOpt("caching",0) 
```
Traceback (most recent call last):
  File "/home/autouser/git/synapse/synapse/eventbus.py", line 154, in dist
    ret.append( func( event ) )
  File "/home/autouser/git/synapse/synapse/lib/config.py", line 135, in callback
    return func(valu)
  File "/home/autouser/git/synapse/synapse/cores/common.py", line 643, in _onSetCaching
    self.cache_byiden.clear()
AttributeError: 'RefDict' object has no attribute 'clear'
```

Discussion:

Patch:
diff --git a/synapse/lib/cache.py b/synapse/lib/cache.py
index 788dc81b5..6c3487c41 100644
--- a/synapse/lib/cache.py
+++ b/synapse/lib/cache.py
@@ -428,5 +428,15 @@ class RefDict:
         with self.lock:
             return [self._pop(k) for k in keys]
 
+    def clear(self):
+        with self.lock:
+            self.vals.clear()
+            self.refs.clear()
+
+    def __contains__(self, item):
+        with self.lock:
+            return item in self.vals
+
     def __len__(self):
-        return len(self.vals)
+        with self.lock:
+            return len(self.vals)


Test patch:
diff --git a/synapse/tests/test_cache.py b/synapse/tests/test_cache.py
index 524d70d61..0c6eb36bf 100644
--- a/synapse/tests/test_cache.py
+++ b/synapse/tests/test_cache.py
@@ -1,9 +1,6 @@
-import unittest
-import threading
 import collections
 
 import synapse.lib.cache as s_cache
-import synapse.cortex as s_cortex
 
 from synapse.tests.common import *
 
@@ -220,3 +217,54 @@ class CacheTest(SynTest):
         od = s_cache.OnDem()
         with self.raises(KeyError) as cm:
             od.get('foo')
+
+    def test_refdict(self):
+        rd = s_cache.RefDict()
+        # puts, pop, get, clear
+        # Put an item in a few times
+        self.eq(rd.put('syn:val', 123), 123)
+        self.true('syn:val' in rd.vals)
+        self.eq(rd.refs.get('syn:val'), 1)
+        rd.put('syn:val', 123)
+        self.true('syn:val' in rd.vals)
+        self.eq(rd.refs.get('syn:val'), 2)
+        # Get values out, ensure defaults of None are returned
+        self.eq(rd.get('syn:val'), 123)
+        self.none(rd.get('syn:noval'))
+        # Pop an item out until its gone from the refdict
+        # XXX Unclear if the following none() is a desired behavior
+        # XXX for pop on a existing value which still has refs > 0
+        self.none(rd.pop('syn:val'))
+        self.eq(rd.refs.get('syn:val'), 1)
+        self.eq(rd.pop('syn:val'), 123)
+        self.false('syn:val' in rd.refs)
+        self.false('syn:val' in rd.vals)
+
+        kvs = (
+            ('foo', 'bar'),
+            ('knight', 'ni'),
+            ('clown', 'pennywise'),
+            ('inet:netuser', 'vertex.link/pennywise')
+        )
+        # puts
+        rd.puts(kvs)
+        # validate setdefault behavior
+        self.eq(rd.put('foo', 'baz'), 'bar')
+        self.eq(rd.refs.get('foo'), 2)
+        # __len__
+        self.eq(len(rd), 4)
+        # __contains__
+        self.true('clown' in rd)
+        self.false('badger' in rd)
+        # gets
+        r = rd.pops(['foo', 'knight', 'syn:novalu'])
+        # self.eq(r, ['bar', 'ni', None])
+        # XXX This is None, ni, None because of the weird pop() behavior.
+        self.eq(r, [None, 'ni', None])
+        self.true('foo' in rd)
+        self.true('knight' not in rd)
+        self.eq(len(rd), 3)
+        # Clear
+        rd.clear()
+        self.eq(len(rd), 0)
+        self.eq(len(rd.refs), 0)
diff --git a/synapse/tests/test_cortex.py b/synapse/tests/test_cortex.py
index 24707a38d..00837e4e8 100644
--- a/synapse/tests/test_cortex.py
+++ b/synapse/tests/test_cortex.py
@@ -1279,6 +1279,26 @@ class CortexTest(SynTest):
             self.true(tufo0[1].get('.new'))
             self.false(tufo1[1].get('.new'))
 
+    def test_cortex_caching_disable(self):
+
+        with s_cortex.openurl('ram://') as core:
+
+            core.setConfOpt('caching', 1)
+
+            tufo = core.formTufoByProp('foo', 'bar')
+
+            self.nn(core.cache_byiden.get(tufo[0]))
+            self.nn(core.cache_bykey.get(('foo', 'bar', 1)))
+            self.nn(core.cache_byprop.get(('foo', 'bar')))
+            self.eq(len(core.cache_fifo), 1)
+
+            core.setConfOpt('caching', 0)
+
+            self.none(core.cache_byiden.get(tufo[0]))
+            self.none(core.cache_bykey.get(('foo', 'bar', 1)))
+            self.none(core.cache_byprop.get(('foo', 'bar')))
+            self.eq(len(core.cache_fifo), 0)
+
     def test_cortex_reqstor(self):
         with s_cortex.openurl('ram://') as core:
             self.raises(BadPropValu, core.formTufoByProp, 'foo:bar', True)