MCPcopy Create free account
hub / github.com/dwavesystems/dwave-cloud-client / MemoryCache

Class MemoryCache

tests/api/mocks.py:342–373  ·  view source on GitHub ↗

In-memory cache with API partially compatible with ``diskcache``. Implemented as a thin wrapper around ``dict`` that adds a subset of diskcache API that's used in the cloud-client. Namely, methods: ``.get(..., read: bool)`` and `.set(..., read: bool)``.

Source from the content-addressed store, hash-verified

340
341
342class MemoryCache(dict):
343 """In-memory cache with API partially compatible with ``diskcache``.
344
345 Implemented as a thin wrapper around ``dict`` that adds a subset of diskcache
346 API that's used in the cloud-client. Namely, methods: ``.get(..., read: bool)``
347 and `.set(..., read: bool)``.
348 """
349
350 def get(self, key, default=None, read=False, **kwargs):
351 """Retrieve value from cache. If `key` is missing, return `default`.
352
353 When `read` is True, return a file handle to value.
354
355 Note: other diskcache arguments are ignored.
356 """
357 value = super().get(key, default)
358 if value is not None and read:
359 value = io.BytesIO(value)
360 return value
361
362 def set(self, key, value, read=False, **kwargs):
363 """Set `key` and `value` item in cache.
364
365 When `read` is `True`, `value` should be a file-like object opened
366 for reading in binary mode.
367
368 Note: other diskcache arguments are ignored.
369 """
370 if read:
371 value = value.read()
372 self[key] = value
373 return True

Calls

no outgoing calls