Other than the modules already suggested, Tie::Cache looks like a close match to what you want. It works on the least recently used concept allowing you to specify limits based on quantity and size. I also rolled my own because it looked like fun:
#!/usr/bin/perl package Fixed_Hash; require Tie::Hash; @ISA = (Tie::StdHash); my ($used, $max, %cache); sub TIEHASH { my $class = shift; my $limit = shift || 100; $max = int ( $limit / 2 ); bless {} , $class; } sub FETCH { my ($self, $key) = @_; return exists $cache{$key} ? $cache{$key} : $self->{$key}; } sub STORE { my ($self, $key, $val) = @_; return if exists $cache{$key} && $cache{$key} eq $val; return if exists $self->{$key} && $self->{$key} eq $val; ++$used; if ( $used >= $max ) { %cache = %{ $self }; %{ $self } = ($key, $val); $used = 1; } else { $self->{$key} = $val; } return; } package main; use strict; use warnings; tie my %fixed_hash, 'Fixed_Hash', 20;
Cheers - L~R
In reply to Re: how to limit size of hash
by Limbic~Region
in thread how to limit size of hash
by albert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |