The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tie::Hash::Expire - A tied hash object with key-value pairs that expire.

SYNOPSIS

    use Tie::Hash::Expire;

    # Use the default time() function from the system or from Time::HiRes if
    # that module is available.
    tie my %foo => 'Tie::Hash::Expire', LIFETIME => 10;
    $foo{bar} = 1;
    sleep(10);
    # $foo{bar} no longer exists.

    # Explicitly do not try to import Time::HiRes::time().
    tie my %bar => 'Tie::Hash::Expire', HIRES => 0, LIFETIME => 10;

    # Use a manual, counter-style time function.
    sub my_time_closure
    {
        my $my_time = 0;

        return sub
        {
            my ($add) = @_;

            if (defined $add)
            {
                $my_time += $add;
            }

            return $my_time;
        };
    }
    my $f = my_time_closure();
    tie my %baz => 'Tie::Hash::Expire', TIMEFUNC => $f, LIFETIME => 10;
    $bar{a} = 1;
    $f->(9);
    # $bar{a} still exists.
    $f->(1);
    # $bar{a} no longer exists.

    # If you don't specify a LIFETIME or if your LIFETIME is <= 0, then you
    # just get an ordinary (i.e. not tied to anything) hash back.
    tie my %qux => 'Tie::Hash::Expire', LIFETIME => 0;
    # %qux is just an empty list: ().

METHODS

TIEHASH

Creates the hash, with an optional LIFETIME parameter. If LIFETIME is undefined or zero, you would be better off using an ordinary hash instead of this fancy, tied type; however, this module will play along, anyway.

FETCH

Returns the value corresponding to a key. If the key does not exist in the underlying hash, returns undef.

STORE

Stores a key-value pair in the underlying hash. Sets the expiry time to the current time plus LIFETIME. If the key had previously been scheduled for deletion, unschedules it.

EXISTS

Returns a true value if the key exists in the underlying hash or false if the key does not exist in the underlying hash.

CLEAR

Resets the underlying hash and all expiry times. Also removes all scheduled deletion records.

DELETE

Deletes a key-value pair from the underlying hash.

SCALAR

Returns the scalar value of the underlying hash.

FIRSTKEY

Returns the first key in the hash, for iterative purposes.

NEXTKEY

Returns the next key in the hash, using the iterator first assigned in FIRSTKEY() or later reassigned in a previous NEXTKEY() call as a basis for search.

CAVEATS

If the user does not explicitly provide a function for TIMEFUNC, this module will use the Perl time() function to determine whether a key-value pair is expired. This means that any oddities in the system clock may unexpectedly affect the performance of this module. In particular, if a service such as NTP regularly sets the system time and happens to frequently compensate for significant clock drift, unexpected results will definitely occur.

This module will attempt to use the time() from Time::HiRes if that module can be found. Otherwise, it will fall back to the core Perl time() function, which does not provide sub-second accuracy.

Tie::Hash::Expire does not inherently understand the concept of a non-expiring hash; however, if it's absolutely necessary to implement one of these with Tie::Hash::Expire, a TIMEFUNC that returns a constant could be used.

AUTHOR

Colin Wetherbee <cww@cpan.org>

BUGS

Please report any bugs or feature requests to bug-tie-hash-expire at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tie-Hash-Expire. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Tie::Hash::Expire

You can also look for information at:

COPYRIGHT

Copyright (c) 2009 Colin Wetherbee

LICENSE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.