internal package Foswiki::Contrib::CacheContrib


Interface to the caching services. This consists of two parts:

  1. basic caching: cache computational results for a short period of time for faster access
  2. a caching user agent: fetch external resources and cache them

Both of these requirements happen so often in Foswiki plugins that they have been provided as a basic service to be accessed by third party plugins. For instance, ImagePlugin caches image geometries as analysing and extracting this information from pictures can be quite expensive. NumberPlugin fetches exchange rates of currencies from an external provider and caches them locally. FeedPlugin fetches RSS and Atom feeds and caches them locally when rerendering them on a Foswiki page. SolrPlugin serializes binary document formats while indexing their content with interim results cached locally to speed up reindexing those documents.

getUserAgent($namespace) -> $ua

returns a singleton caching user agent compatible to CPAN:LWP::UserAgent. The optional namespace (defaults to "UserAgent") parameter defines the cache section used for this agent.

getCache($namespace, $expire) -> $cache

returns a CHI cache object for the given namespace.

my $cache = Foswiki::Contrib::CacheContrib::getCache("ImagePlugin");

getExternalResource($url, ...) -> $response

Fetch an external resource using the caching UserAgent. This is equivalent to Foswiki::Func::getExternalResource() with just adding caching. It basically is compatible with LWP::UserAgent::get.


my $response = Foswiki::Contrib::CacheContrib::getExternalResource($url);

throw Error::Simple("http error fetching $url: ".$response->code." - ".$response->status_line)
  unless $response->is_success;

my $content = $response->decoded_content();


clears the cache for the given namespace


purges expired entries of the cache for the given namespace