Module: Cache

The ThreatConnect TcEx App Framework provides a simple interface for caching data in Apps utilizing the ThreatConnect DataStore feature. The Cache module provides an interface to get, add, update, and delete cache data.

DataStore Instance

The tcex.cache() module provides persistent storage for Apps. The domain value specifies access level for the data as described in the following section.

Domains

  • local - limited to the specific App.

  • organization - limited to the current Org.

1cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')

Add Record

The add() method allows record data to be added. The rid (resource id) is an identifier for the provided data.

1cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')
2response = cache.add(rid='one', data={'one': 1})

Delete Record

The delete() method allows record data to be deleted using the rid (resource id) value.

1cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')
2response = cache.delete(rid='one')

Get Record

The get() method allows record data to be retrieved using the rid (resource id) value.

1cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')
2response = cache.get(rid='one', raise_on_error=True)

The get() method also allows a callback to be provided, which will be called if the cached data is expired to automatically update the cache. In the following example if the cached data is expired the callback method will be called passing the rid (resource id).

Note

The callback method should return a dict value to be cached.

1def expired_data_callback(rid: str) -> dict:
2    """Return dummy data for cache callback."""
3    sample_data = {'my_data': {'data': 'updated data to cache'}}
4    return sample_data.get(rid)
5
6cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')
7response = cache.get(rid='one', data_callback=expired_data_callback, raise_on_error=True)

Update Record

The update() method allows record data to be updated/overwritten with new data.

1cache = self.tcex.cache(domain='local', data_type='myDnsData', ttl_seconds='180')
2response = cache.update(rid='one', data={'one': 1})