Hey::Cache - Cache data multiple data structures


Hey-Cache documentation  | view source Contained in the Hey-Cache distribution.

Index


NAME

Top

Hey::Cache - Cache data multiple data structures

SYNOPSIS

Top

  use Hey::Cache;

  my $cache = Hey::Cache->new(
      Namespace => 'WeatherApp2000',		# string (optional, default='default')
      CacheFile => 'fun_cache_file.xml',	# file path and name (optional, default='cache.xml')
      AutoSync => 1,				# boolean (optional, default=1)
      Expires => 300, 				# seconds (optional, default=86400)
  );

  $cache->set(
      Name => '98501',				# sets the name/key of the piece of data
      Value => { Temperature => 17,		# sets the data that you wish to cache
		 Condition => 'Rain',
		 High => 19,
		 Low => 7 },
      Expires => 600, 				# optional, defaults to what was set in the constructor above
  );

  $value = $cache->get( Name => '98501' ); 	# returns what you had set

  ... enough time passes (at least 10 minutes, according to the "Expires" value) ...

  $value = $cache->get( Name => '98501' ); 	# returns undef because it has expired

  $value = $cache->get( Name => '98501', Expires => 86400 );	# returns what you had set
								# because it is newer than a day

DESCRIPTION

Top

Helps with regular data caching. It's targetted for items that are in hash references, primarly.

new

  my $cache = Hey::Cache->new(
      Namespace => 'WeatherApp2000',		# string (optional, default='default')
      CacheFile => 'fun_cache_file.xml',	# file path and name (optional, default='cache.xml')
      AutoSync => 1,				# boolean (optional, default=1)
      Expires => 300, 				# seconds (optional, default=86400)
  );

Namespace [optional]

Default value is "default".

CacheFile [optional]

Default value is "cache.xml".

AutoSync [optional]

Default value is 1.

Expires [optional]

Default value is 86400 (24 hours).

sync

  $cache->sync;

Sends the data out to file. If AutoSync is disabled (per call or in the constructor), this will manually save out your data to the cache file. If AutoSync is enabled, this will happen automatically.

get

  $weather = $cache->get( Name => '98501' );
  $weather = $cache->get( Name => '98501', Expires => 600 ); # override the expiration of the item

Gets the named data from the cache.

Name [required]

The name of the item to return. This name was specified in the $cache->set function.

Expires [optional]

Age in number of seconds that would be acceptable. If the cached item is newer than this value, it will return the item. If the cached item is older than the value, it will return undef.

set

  $value = { Temperature => 14, High => 15, Low => 12 };
  $cache->get( Name => '98501', Value => $value );

Set a value (scalar, hash, etc) by name into the cache.

Name [required]

The name. Name of the item. Use this as a key to get it later with the $cache->get function.

Value [required]

The value. It works best if it's a reference to something, especially a hash.

Sync [optional]

Boolean. Defaults to the value specified in the constructor, which defaults to true.

Timestamp [optional]

Defaults to the current time. If it is useful to set a different timestamp, you can do it here. This value is in epoch seconds.

Expires [optional]

Defaults to the value specified in the constructor. Sets the expiration time for this item. Expiration is stored with each item separately, so you can assign different expirations for different items.

AUTHOR

Top

Dusty Wilson, <hey-cache-module@dusty.hey.nu>

COPYRIGHT AND LICENSE

Top


Hey-Cache documentation  | view source Contained in the Hey-Cache distribution.