52
edits
No edit summary |
|||
| Line 152: | Line 152: | ||
=== Making the most of noun caching === | === Making the most of noun caching === | ||
Ubiquity has a caching feature which allows it to remember the suggestions that a noun type gave for a particular input. The tricky part of this caching is in deciding when to flush the cached results for each noun type. The length of time that a noun | Ubiquity has a caching feature which allows it to remember the suggestions that a noun type gave for a particular input. The tricky part of this caching is in deciding when to flush the cached results for each noun type. The length of time that a noun type's results remain accurate can differ greatly depending on the nature of the noun type. For instance, a noun type based on your open tabs may only remain accurate for a few seconds, while a noun type for street addresses will probably remain accurate for weeks/months, and a noun type that is strictly regex based will be accurate forever. There are two ways for a noun type to declare when it should be flushed: the cacheTime property and and the registerCacheObserver method. | ||
'''The cacheTime property''' | '''The cacheTime property''' | ||
By default, a noun | By default, a noun type's results will be cached for one day. However, you can specify a different duration for the cache by adding the cacheTime property to your noun type with some integer value that represents the length of time to keep results cached in seconds. A cacheTime of 0 will result in no caching, while a cacheTime of -1 will result in the results being cached forever. | ||
<pre> | <pre> | ||
// Example: Cache results for 1 minute | // Example: Cache results for 1 minute | ||
| Line 170: | Line 170: | ||
'''The registerCacheObserver method''' | '''The registerCacheObserver method''' | ||
While the cacheTime property allows noun types to specify a fixed amount of time between flushings of the cache, for some noun types it makes more sense to have the cache flushed on certain events. For example, noun_type_tab flushes it's cache every time a new tab is opened or an old tab is closed. In order to register observers for flushing, simply add the registerCacheObserver property to your noun type. This property should be a function which takes a single argument that is a flush function. | While the cacheTime property allows noun types to specify a fixed amount of time between flushings of the cache, for some noun types it makes more sense to have the cache flushed on certain events. For example, noun_type_tab flushes it's cache every time a new tab is opened or an old tab is closed. In order to register observers for flushing, simply add the registerCacheObserver property to your noun type. This property should be a function which takes a single argument that is a flush function. Within registerCacheObserver you can set event listeners which call this flush function anytime you want to flush the cache of the noun type. | ||
<pre> | <pre> | ||
// Example | // Example | ||
| Line 184: | Line 184: | ||
}; | }; | ||
</pre> | </pre> | ||
=== Best practices === | === Best practices === | ||
edits