The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DB::Object::Cache::Tables - Table Cache

SYNOPSIS

    my $cache = DB::Object::Cache::Tables->new({
        timeout => 86400,
        # This is automatically set
        # cache_file => '/some/dir/sql_tables.json',
    });
    $dbh->cache_tables( $cache_tables );
    $tables = $dbh->tables_info;
    my $cache = 
    {
    host => $host,
    driver => $driver,
    port => $port,
    database => $database,
    tables => $tables,
    };
    if( !defined( $cache->set( $cache ) ) )
    {
        warn( "Unable to write to tables cache: ", $cache->error, "\n" );
    }
    
    # Returning an array reference of tables hash reference definition
    $all = $cache_tables->get({
        host => $self->host,
        driver => $self->driver,
        port => $self->port,
        database => $db,
    }) || do
    

VERSION

    v0.100.3

DESCRIPTION

This is a simple given to maintain a cache of database tables in a session. When a connection object is created, it will issue a query to get the list of all tables and views in the database and pass it to DB::Object::Cache::Tables, and save its object. It is then used later several times such as when instantiating table objects.

METHODS

init

Possible parameters:

cache_dir

An absolute path to a directory that will contain the json cache file. Beware that if you run your script from the web, this directory must be writable by the http server user.

cache_file

Alternatively to cache_dir, you can provide an absolute path to the json cache file.

timeout

An amount of time in second until the cache file becomes obsolete.

cache

Returns the hash reference structure of the cache

cache_dir

Set or get the cache dir.

When set, this will also set the cache file calling "cache_file"

cache_file

Set or get the cache file.

When set, this will store the cache file modification time to check later if it has become obsolete and load its json data into the "cache"

get

Given an hash reference of parameters, this will return an array reference of table hash reference.

Parameters are:

host
driver
port
database

read

Given a full path to a json cache file, this will read the file and return its data as a hash reference.

If an error occurs while reading the json cache file, it will issue a warning using warn and return an empty hash reference.

set

Provided with an hash reference of parameters, this will add it to the cache data and write it to the file.

Parameters are:

host
driver
port
database
tables

An array reference of hash reference containing table definition as returned by DB::Object::table_info

timeout

Set/get the cache file timeout.

If the current unix timestamp minus the cache file timestamp is higher than the timeout, the cache file has expired.

updated

Set/get the cache file last modified unix timestamp

write

Provided with a cache data, which is a hash reference and optionally the full path to the cache file, and write will write the hash data as a json to the cache file.

If no cache file is provided as a second argument, it will use the default one set up when the object was instantiated.

It returns the size of the cache file or return undef and set the error

COPYRIGHT

Copyright (c) 2000-2019 DEGUEST Pte. Ltd.

SEE ALSO

DB::Object

AUTHOR

Jacques Deguest <jack@deguest.jp>

COPYRIGHT & LICENSE

Copyright (c) 2018-2021 DEGUEST Pte. Ltd.

You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.