About non-existing cache definitions


#1

Hey there!

When trying to get or set a cache entry from the code, using the cache API, the cache definition needs to exist before anything is allowed to happen:

cache = self.cache.get_cache('builtin', 'bookings')

If one has created them using the web admin, as it is expected, everything works fine. If not, you get a KeyError:

2018-11-14 11:01:36,836 - WARNING - 136:DummyThread-50 - zato.server.service:502 - Traceback (most recent call last):
  File "/opt/zato/3.0/code/zato-server/src/zato/server/service/__init__.py", line 477, in update_handle
    self._invoke(service, channel)
  File "/opt/zato/3.0/code/zato-server/src/zato/server/service/__init__.py", line 399, in _invoke
    service.handle()
  File "/opt/zato/env/qs-1/server1/work/hot-deploy/current/guest.py", line 511, in handle
    cache = self.cache.get_cache('builtin', 'bookings')
  File "/opt/zato/3.0/code/zato-server/src/zato/server/connection/cache.py", line 849, in get_cache
    return self.caches[cache_type][name]
KeyError: u'bookings'

2018-11-14 11:01:36,838 DEBG 'zato-server1' stdout output:
2018-11-14 11:01:36,837 - ERROR - 136:DummyThread-50 - zato.server.connection.http_soap.channel:324 - Caught an exception, cid:`7429edfa8234f5e9d8e4e3f4`, status_code:`500`, _format_exc:`Traceback (most recent call last):
  File "/opt/zato/3.0/code/zato-server/src/zato/server/connection/http_soap/channel.py", line 268, in dispatch
    payload, worker_store, self.simple_io_config, post_data, path_info, soap_action)
  File "/opt/zato/3.0/code/zato-server/src/zato/server/connection/http_soap/channel.py", line 502, in handle
    params_priority=channel_item.params_pri)
  File "/opt/zato/3.0/code/zato-server/src/zato/server/service/__init__.py", line 477, in update_handle
    self._invoke(service, channel)
  File "/opt/zato/3.0/code/zato-server/src/zato/server/service/__init__.py", line 399, in _invoke
    service.handle()
  File "/opt/zato/env/qs-1/server1/work/hot-deploy/current/guest.py", line 511, in handle
    cache = self.cache.get_cache('builtin', 'bookings')
  File "/opt/zato/3.0/code/zato-server/src/zato/server/connection/cache.py", line 849, in get_cache
    return self.caches[cache_type][name]
KeyError: u'bookings'

I was wondering whether it would be useful to allow the self.cache.get_cache method to have an additonal parameter create_cache (or similar) to allow creation of such definition with default parameter values and prevent the error from happening.

What do you think, @dsuch?

Thanks in advance.


#2

This is feasible but I am just pondering myself if it would not mean that other places, some connection types perhaps, should not work for consistence in a way similar to it, that is, create on first use if something does not exist.

But it looks that built-in caches are special, to a degree, in that it is possible to create them without any user input, though in this case they would assume default values for all the options.