Cache::CacheFactory::Expiry::Time - Time-based expiry policy for Cache::CacheFactory.


Cache-CacheFactory documentation Contained in the Cache-CacheFactory distribution.

Index


Code Index:

NAME

Top

Cache::CacheFactory::Expiry::Time - Time-based expiry policy for Cache::CacheFactory.

DESCRIPTION

Top

Cache::CacheFactory::Expiry::Time is a time-based expiry (pruning and validity) policy for Cache::CacheFactory.

It provides similar functionality and backwards-compatibility with the $expires_in and $default_expires_in properties of Cache::Cache.

INTERVAL SPECIFICATIONS

Top

You can use any of the syntaxes provided by Cache::Cache to specify an interval for expiry times, for example:

  $Cache::Cache::EXPIRES_NEVER
  $Cache::Cache::EXPIRES_NOW
  '4 seconds'
  '1 m'
  'now'
  'never'

For a full list take a look at the set() section of the Cache::Cache documentation.

STARTUP OPTIONS

Top

The following startup options may be supplied to Cache::CacheFactory::Expiry::Time, see the Cache::CacheFactory documentation for how to pass options to a policy.

default_prune_after => $interval

For a pruning policy this sets the default interval after which an item becomes eligible to be pruned.

default_valid_until => $interval

For a validity policy this sets the default time interval after which an item should be considered invalid.

default_expires_in => $interval

This option provides backwards-compatibility with Cache::Cache, it sets default_prune_after for pruning policies and default_valid_until for validity policies.

STORE OPTIONS

Top

The following options may be set when storing a key, see the Cache::CacheFactory documentation for details on how to do this.

prune_after => $interval

For a pruning policy this sets the interval after which the item becomes eligible to be pruned. If not supplied then the value of default_prune_after in the startup options will be used.

valid_until => $interval

For a validity policy this sets the time interval after which the item should be considered invalid. If not supplied then the value of default_valid_until in the startup options will be used.

expires_in => $interval

This option provides backwards-compatibility with Cache::Cache, it behaves as prune_after for pruning policies and valid_until for validity policies.

METHODS

Top

You should generally call these via the Cache::CacheFactory interface rather than directly.

$policy->set_default_expires_in( $default_expires_in );
$default_expires_in = $policy->get_default_expires_in();

Set or get the default_expires_in option.

SEE ALSO

Top

Cache::CacheFactory, Cache::Cache, Cache::CacheFactory::Object, Cache::CacheFactory::Expiry::Base

AUTHORS

Top

Original author: Sam Graham <libcache-cachefactory-perl BLAHBLAH illusori.co.uk>

Last author: $Author: illusori $

COPYRIGHT

Top


Cache-CacheFactory documentation Contained in the Cache-CacheFactory distribution.

###############################################################################
# Purpose : Cache Time Expiry Policy Class.
# Author  : Sam Graham
# Created : 25 Jun 2008
# CVS     : $Id: Time.pm,v 1.8 2010-02-16 12:25:41 illusori Exp $
###############################################################################

package Cache::CacheFactory::Expiry::Time;

use warnings;
use strict;

use Cache::Cache;
use Cache::BaseCache;

use Cache::CacheFactory::Expiry::Base;

use base qw/Cache::CacheFactory::Expiry::Base/;

$Cache::CacheFactory::Expiry::Time::VERSION = '1.10';

sub read_startup_options
{
    my ( $self, $param ) = @_;

    $self->set_default_expires_in( $param->{ default_expires_in } )
        if exists( $param->{ default_expires_in } );

    $self->{ default_prune_after } = $param->{ default_prune_after }
        if exists( $param->{ default_prune_after } );
    $self->{ default_valid_until } =
        ( $param->{ default_valid_until } eq 'forever' ) ?
        $Cache::Cache::EXPIRES_NEVER : $param->{ default_valid_until }
        if exists( $param->{ default_valid_until } );
}

sub set_default_expires_in
{
    my ( $self, $default_expires_in ) = @_;

    #  Compat with Cache::Cache.
    $self->{ default_prune_after } = $default_expires_in;
    $self->{ default_valid_until } = $default_expires_in;
}

sub get_default_expires_in
{
    my ( $self ) = @_;

    return( $self->{ default_prune_after } || $self->{ default_valid_until } );
}

sub set_object_validity
{
    my ( $self, $key, $object, $param ) = @_;
    my ( $valid_until );

    #  Failover in order:
    #    supplied valid_until param
    #    supplied expires_in param
    #    default valid_until param (includes default expires_in)
    #    EXPIRES_NEVER as last resort.
    $valid_until = $self->{ default_valid_until }
        if exists( $self->{ default_valid_until } );
    $valid_until = $param->{ expires_in }
        if exists( $param->{ expires_in } );
    $valid_until =
        ( $param->{ valid_until } eq 'forever' ) ?
        $Cache::Cache::EXPIRES_NEVER : $param->{ valid_until }
        if exists( $param->{ valid_until } );

    $valid_until = Cache::BaseCache::Build_Expires_At(
        $object->get_created_at(),
        $Cache::Cache::EXPIRES_NEVER,
        $valid_until );

    $object->set_policy_metadata( 'validity', 'time',
        { valid_until => $valid_until, } );
}

sub set_object_pruning
{
    my ( $self, $key, $object, $param ) = @_;
    my ( $prune_after );

    #  Failover in order:
    #    supplied prune_after param
    #    supplied expires_in param
    #    default prune_after param (includes default expires_in)
    #    EXPIRES_NEVER as last resort.
    $prune_after = $self->{ default_prune_after }
        if exists( $self->{ default_prune_after } );
    $prune_after = $param->{ expires_in }
        if exists( $param->{ expires_in } );
    $prune_after = $param->{ prune_after }
        if exists( $param->{ prune_after } );

    $prune_after = Cache::BaseCache::Build_Expires_At(
        $object->get_created_at(),
        $Cache::Cache::EXPIRES_NEVER,
        $prune_after );

    $object->set_policy_metadata( 'pruning', 'time',
        { prune_after => $prune_after, } );
}

sub should_keep
{
    my ( $self, $cache, $storage, $policytype, $object ) = @_;
    my ( $metadata, $expires, $when );

    $metadata = $object->get_policy_metadata( $policytype, 'time' );
    $expires  = $metadata->{ valid_until } || $metadata->{ prune_after };
    $when     = time();

    return( 1 ) unless defined( $expires );
    return( 0 ) if $expires eq $Cache::Cache::EXPIRES_NOW;
    return( 1 ) if $expires eq $Cache::Cache::EXPIRES_NEVER;
    return( 0 ) if $when >= $expires;
    return( 1 );
}

1;