6 Restrictions on arguments to cacheable components
If you want to call a component with cache=yes (or
cache=defer or cache=force), there is one
restriction you must obey:
- The attributes passed to the cached component must be,
as Python says, hashable.
The reason for this restriction is that the SkunkWeb server
needs to take all of the attributes you pass and flatten
them into a short, unique value. The SkunkWeb server uses
this value to look up the cached output it has stored in
its disk cache.
So, what does hashable mean? Well, just consider it
to mean ``able to be flattened'' until you learn more Python.
Here's a rule of thumb:
- Numbers, strings, dates, and the None object are hashable
- Tuples (read-only sequences) are hashable if all of its items
are hashable
- Lists (changeable sequences) are not hashable, but SkunkWeb makes
an exception and lets you use them as arguments to cacheable
components, as long as the list's items are either hashable or
are lists or dictionaries. However, you must be very careful that the called
component does not try to change the list in any way; if it does,
you will find that the list changes only the first time the component
is called, becuase on subequent calls, the output is cached and the
component will not be executed.
- Dictionaries are not hashable, but SkunkWeb makes an exception for them
just as it does for lists, with the same cautions and restrictions.
- Any combination of acceptable objects is acceptable:
a tuple of lists of dates, etc.
- DateTime objects are not natively hashable, but
SkunkWeb has a provision in the cache key generation code to make it
as if they were hashable.
- Instances of Python classes which you write yourself are not
acceptable, unless you implement a __hash__ or __cachekey__
method in the class. Consult the Skunk mailing list if you want
to make instances of your own classes acceptable arguments to
cacheable components.