Example #1
0
    def test_put_get_expires(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        ctrl.put(NODE, 10, "key1", expires="1,D")
        ctrl.put(NODE, 10, "key2", expires="-1,D")
        assert ctrl.get(NODE, "key1") == 10
        with pytest.raises(CacheException,
                           match="Requested data is not in any cache stores"):
            ctrl.get(NODE, "key2")
Example #2
0
    def test_put_has_expires(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        ctrl.put(NODE, 10, "key1", expires="1,D")
        ctrl.put(NODE, 10, "key2", expires="-1,D")
        assert ctrl.has(NODE, "key1")
        assert not ctrl.has(NODE, "key2")
Example #3
0
    def test_invalid_coordinates(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # type
        with pytest.raises(TypeError, match="Invalid coordinates"):
            ctrl.put(NODE, 10, "key", coordinates="coords")

        with pytest.raises(TypeError, match="Invalid coordinates"):
            ctrl.get(NODE, "key", coordinates="coords")

        with pytest.raises(TypeError, match="Invalid coordinates"):
            ctrl.has(NODE, "key", coordinates="coords")

        with pytest.raises(TypeError, match="Invalid coordinates"):
            ctrl.rem(NODE, "key", coordinates="coords")
Example #4
0
    def test_invalid_node(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # type
        with pytest.raises(TypeError, match="Invalid node"):
            ctrl.put("node", 10, "key")

        with pytest.raises(TypeError, match="Invalid node"):
            ctrl.get("node", "key")

        with pytest.raises(TypeError, match="Invalid node"):
            ctrl.has("node", "key")

        with pytest.raises(TypeError, match="Invalid node"):
            ctrl.rem("node", "key")
Example #5
0
    def test_rem_wildcard_key(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # put and check has
        ctrl.put(NODE, 10, "key")
        assert ctrl.has(NODE, "key")

        # rem other and check has
        ctrl.rem(NODE, item="*")
        assert not ctrl.has(NODE, "key")
Example #6
0
    def test_init_list(self):
        ctrl = CacheCtrl(cache_stores=[])
        assert len(ctrl._cache_stores) == 0
        assert ctrl.cache_stores == []
        repr(ctrl)

        ctrl = CacheCtrl(cache_stores=[RamCacheStore()])
        assert len(ctrl._cache_stores) == 1
        assert isinstance(ctrl._cache_stores[0], RamCacheStore)
        assert ctrl.cache_stores == ["ram"]
        repr(ctrl)

        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])
        assert len(ctrl._cache_stores) == 2
        assert isinstance(ctrl._cache_stores[0], RamCacheStore)
        assert isinstance(ctrl._cache_stores[1], DiskCacheStore)
        assert ctrl.cache_stores == ["ram", "disk"]
        repr(ctrl)
Example #7
0
    def test_put_clear(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # put and check has
        ctrl.put(NODE, 10, "key")
        assert ctrl.has(NODE, "key")

        # clear and check has
        ctrl.clear()

        # check has
        assert not ctrl.has(NODE, "key")
Example #8
0
    def test_partial_has_get(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # has False
        assert not ctrl._cache_stores[0].has(NODE, "key")
        assert not ctrl._cache_stores[1].has(NODE, "key")
        assert not ctrl.has(NODE, "key")

        # put only in disk
        ctrl._cache_stores[1].put(NODE, 10, "key")

        # has
        assert not ctrl._cache_stores[0].has(NODE, "key")
        assert ctrl._cache_stores[1].has(NODE, "key")
        assert ctrl.has(NODE, "key")

        # get
        with pytest.raises(CacheException, match="Cache miss"):
            ctrl._cache_stores[0].get(NODE, "key")
        assert ctrl._cache_stores[1].get(NODE, "key") == 10
        assert ctrl.get(NODE, "key") == 10
Example #9
0
    def test_cleanup(self):
        from podpac.core.cache.ram_cache_store import _thread_local

        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        ctrl.put(NODE, 10, "key1", expires="1,D")
        ctrl.put(NODE, 10, "key2", expires="-1,D")

        # 2 entries (even though one is expired)
        assert len(_thread_local.cache) == 2
        assert len(ctrl._cache_stores[1].search(NODE)) == 2

        ctrl.cleanup()

        # only 1 entry (the expired entry has been removed)
        assert len(_thread_local.cache) == 1
        assert len(ctrl._cache_stores[1].search(NODE)) == 1
Example #10
0
    def test_put_has_get(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # has False
        assert not ctrl._cache_stores[0].has(NODE, "key")
        assert not ctrl._cache_stores[1].has(NODE, "key")
        assert not ctrl.has(NODE, "key")

        # put
        ctrl.put(NODE, 10, "key")

        # has True
        assert ctrl._cache_stores[0].has(NODE, "key")
        assert ctrl._cache_stores[1].has(NODE, "key")
        assert ctrl.has(NODE, "key")

        # get value
        assert ctrl._cache_stores[0].get(NODE, "key") == 10
        assert ctrl._cache_stores[1].get(NODE, "key") == 10
        assert ctrl.get(NODE, "key") == 10
Example #11
0
 def _eval(self, coordinates, output=None, _selector=None):
     node = Interpolate(
         interpolation=self.interpolation, source_id=self.hash, force_eval=True, cache_ctrl=CacheCtrl([])
     )
     node._set_interpolation()
     selector = node._interpolation.select_coordinates
     node._source_xr = super()._eval(coordinates, _selector=selector)
     self._interp_node = node
     r = node.eval(coordinates, output=output)
     # Helpful for debugging
     self._from_cache = node._from_cache
     return r
Example #12
0
    def test_put_rem(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # put and check has
        ctrl.put(NODE, 10, "key")
        assert ctrl.has(NODE, "key")

        # rem other and check has
        ctrl.rem(NODE, "other")
        assert ctrl.has(NODE, "key")

        # rem and check has
        ctrl.rem(NODE, "key")
        assert not ctrl.has(NODE, "key")
Example #13
0
 def test_init_default(self):
     ctrl = CacheCtrl()
     assert len(ctrl._cache_stores) == 0
     assert ctrl.cache_stores == []
     repr(ctrl)
Example #14
0
    def test_invalid_mode(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        with pytest.raises(ValueError, match="Invalid mode"):
            ctrl.put(NODE, 10, "key", mode="other")

        with pytest.raises(ValueError, match="Invalid mode"):
            ctrl.get(NODE, "key", mode="other")

        with pytest.raises(ValueError, match="Invalid mode"):
            ctrl.has(NODE, "key", mode="other")

        with pytest.raises(ValueError, match="Invalid mode"):
            ctrl.rem(NODE, "key", mode="other")

        with pytest.raises(ValueError, match="Invalid mode"):
            ctrl.clear(mode="other")
Example #15
0
    def test_put_has_mode(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # put disk and check has
        assert not ctrl.has(NODE, "key")

        ctrl.put(NODE, 10, "key", mode="disk")
        assert not ctrl._cache_stores[0].has(NODE, "key")
        assert not ctrl.has(NODE, "key", mode="ram")
        assert ctrl._cache_stores[1].has(NODE, "key")
        assert ctrl.has(NODE, "key", mode="disk")
        assert ctrl.has(NODE, "key")

        # put ram and check has
        ctrl.clear()
        assert not ctrl.has(NODE, "key")

        ctrl.put(NODE, 10, "key", mode="ram")
        assert ctrl._cache_stores[0].has(NODE, "key")
        assert ctrl.has(NODE, "key", mode="ram")
        assert not ctrl._cache_stores[1].has(NODE, "key")
        assert not ctrl.has(NODE, "key", mode="disk")
        assert ctrl.has(NODE, "key")
Example #16
0
    def test_invalid_key(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        # type
        with pytest.raises(TypeError, match="Invalid item"):
            ctrl.put(NODE, 10, 10)

        with pytest.raises(TypeError, match="Invalid item"):
            ctrl.get(NODE, 10)

        with pytest.raises(TypeError, match="Invalid item"):
            ctrl.has(NODE, 10)

        with pytest.raises(TypeError, match="Invalid item"):
            ctrl.rem(NODE, 10)

        # wildcard
        with pytest.raises(ValueError, match="Invalid item"):
            ctrl.put(NODE, 10, "*")

        with pytest.raises(ValueError, match="Invalid item"):
            ctrl.get(NODE, "*")

        with pytest.raises(ValueError, match="Invalid item"):
            ctrl.has(NODE, "*")

        # allowed
        ctrl.rem(NODE, "*")
Example #17
0
    def test_get_cache_miss(self):
        ctrl = CacheCtrl(cache_stores=[RamCacheStore(), DiskCacheStore()])

        with pytest.raises(CacheException,
                           match="Requested data is not in any cache stores"):
            ctrl.get(NODE, "key")