DBIx::SQLEngine::Record::Trait::Cache - Avoid Repeated Selects
Setup: Several ways to create a class.
my $sqldb = DBIx::SQLEngine->new( ... ); $class_name = $sqldb->record_class( $table_name, undef, 'Cache' ); $sqldb->record_class( $table_name, 'My::Record', 'Cache' ); package My::Record; use DBIx::SQLEngine::Record::Class '-isasubclass', 'Cache'; My::Record->table( $sqldb->table($table_name) );
Cache: Uses Cache::Cache interface.
$class_name->use_cache_style('simple'); # requires Cache::FastMemoryCache $class_name->use_cache_style('active'); use Cache::Cache; $class_name->cache_cache( $my_cache_cache_object );
Basics: Layered over superclass.
# Fetches from cache if it's been seen before $record = $class_name->fetch_record( $primary_key ); # Fetches from cache if we've run this query before @records = $class_name->fetch_select(%clauses)->records; # Clears cache so it's seen by next select query $record->insert_record(); # Clears cache so it's seen by next select query $record->update_record(); # Clears cache so it's seen by next select query $record->delete_record();
This package is not yet complete.
This package provides a caching layer for DBIx::SQLEngine::Record objects.
Don't use this module directly; instead, pass its name as a trait when you create a new record class. This package provides a multiply-composable collection of functionality for Record classes. It is combined with the base class and other traits by DBIx::SQLEngine::Record::Class.
$record_class->cache_cache() : $cache_cache $record_class->cache_cache( $cache_cache )
Cache Object Requirements: This package in intended to work with cache object that use the Cache::Cache interface. However, any package which support the limited cache interface used by this package should be sufficient.
Constructor.
Used to differentiate one cache object from another.
Fetch a value from the cache, if it is present.
Set a value in the cache.
Clear some or all values in the cache.
Included Cache Classes: Two small classes are included that support this interface; see DBIx::SQLEngine::Record::Cache::TrivialCache and DBIx::SQLEngine::Record::Cache::BasicCache.
$record_class->cache_get( $key ) : $value $record_class->cache_get( $key ) : ( $value, $updater_code_ref )
$record_class->cache_set( $key, $value )
$record_class->cache_get_set( $key, $code_ref, @args ) : $value
$record_class->cache_clear() $record_class->cache_clear( $key )
DBIx::SQLEngine->define_cache_styles( $name, $code_ref ) DBIx::SQLEngine->define_cache_styles( %names_and_code_refs )
Define a named caching style.
DBIx::SQLEngine->cache_styles() : %names_and_info DBIx::SQLEngine->cache_styles( $name ) : $info DBIx::SQLEngine->cache_styles( \@names ) : @info DBIx::SQLEngine->cache_styles( $name, $info, ... ) DBIx::SQLEngine->cache_styles( \%names_and_info )
Accessor for global hash mapping cache names to initialization subroutines.
$class_name->use_cache_style( $cache_style_name )
Defaults: The following cache styles are predefined. Except for 'simple', using any of these styles will require installation of the Cache::Cache distribution.
Uses DBIx::SQLEngine::Record::Cache::TrivialCache.
Uses Cache::FastMemoryCache with a default expiration time of 1 seconds.
Uses Cache::FastMemoryCache with a default expiration time of 5 seconds.
Uses Cache::FastMemoryCache with a default expiration time of 30 seconds.
Uses Cache::FileCache with a default expiration time of 30 seconds.
$class_name->fetch_select ( %select_clauses ) : $record_set
$sqldb->fetch_one_record( %select_clauses ) : $record_hash
$class_name->select_record ( $primary_key_value ) : $record_obj $class_name->select_record ( \@compound_primary_key ) : $record_obj $class_name->select_record ( \%hash_with_primary_key_value ) : $record_obj
$class_name->select_records ( @primary_key_values_or_hashrefs ) : $record_set
$class_name->visit_select ( $sub_ref, %select_clauses ) : @results $class_name->visit_select ( %select_clauses, $sub_ref ) : @results
These methods are called internally by the various select methods and do not need to be called directly.
$class_name->record_from_table( $hash_ref )
Calls SUPER method, then cache_records().
$class_name->record_set_from_table( $hash_array_ref )
$class_name->cache_records( @records )
Adds records to the cache.
After constructing a record with one of the new_*() methods, you may save any changes by calling insert_record.
$record_obj->insert_record() : $flag
Attempt to insert the record into the database. Calls SUPER method, so implemented using MIXIN.
Clears the cache.
After retrieving a record with one of the fetch methods, you may save any changes by calling update_record.
$record_obj->update_record() : $record_count
Attempts to update the record using its primary key as a unique identifier. Calls SUPER method, so implemented using MIXIN.
$record_obj->delete_record() : $record_count
Delete this existing record based on its primary key. Calls SUPER method, so implemented using MIXIN.
For more about the Record classes, see DBIx::SQLEngine::Record::Class.
See DBIx::SQLEngine for the overall interface and developer documentation.
See DBIx::SQLEngine::Docs::ReadMe for general information about this distribution, including installation and license information.
To install DBIx::SQLEngine, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::SQLEngine
CPAN shell
perl -MCPAN -e shell install DBIx::SQLEngine
For more information on module installation, please visit the detailed CPAN module installation guide.