albert,
You appear to be in a catch-22. For some reason, the normal solution ($dbh->prepare_cached) is not working and the alternatives will likely defeat the purpose. There are performance impacts using tied interfaces on top of managing your own cache.

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;
This naive approach divides the user defined max between two hashes. When the limit has been reached, the first hash is moved to the second and the first starts over.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.