Red — new-cache

How to create a new cache

Red's Red::Driver::Cache wraps a database driver and materializes SELECT rows. This differs from ResultSeq.cache (materializing one result sequence) and the runtime query-plan cache (compiled plans, not returned rows).

Use the bundled Memory cache

use Red:api<2>;
use Red::Driver::Cache;

my $db = database 'SQLite';
my $*RED-DB = $db;

model Task {
    has Int $.id is serial;
    has Str $.title is column;
}

schema(Task).create;
Task.^create(:title<First>);
$*RED-DB = cache 'Memory' => \(:ttl(30)), $db;
say Task.^all.List;
say Task.^all.List;

cache accepts a cache name or name => Capture configuration and an existing driver, driver name or name => Capture configuration. For example:

my $cached = cache 'Memory' => \(:ttl(30)),
    'SQLite' => \(:database('tasks.sqlite3'));

Create the schema through the underlying driver before installing the cache wrapper.

Memory has an integer TTL of 10 seconds by default. Its data is local to the cache object and expires through a background timer. It tracks tables for invalidation. The bundled CacheInvalidateOnWrite role intercepts inserts only; updates, deletes, raw SQL and writes from other connections/processes do not invalidate it automatically. Expiry is not a transactional consistency guarantee. Use the underlying driver for work requiring current reads or transaction isolation.

Implement an AST cache

Put a new class in lib/Red/Driver/Cache/YourCache.rakumod, register it in your distribution's META6.json, and compose Red::Driver::Cache. The role supplies the required Red::Driver $.driver attribute and delegates basic SQL operations. Implement multi method get-from-cache(Red::AST $ast) and multi method set-on-cache(Red::AST $ast, @rows).

A hit must return a defined iterable of row hashes, including an empty array for an empty result. A miss must return an undefined value such as Nil. The wrapper prepares the underlying query on a miss, consumes its rows, stores them, then returns an iterator over those rows. This eagerly materializes the full result; account for memory usage and large queries.

Here is a loadable, deliberately minimal cache that always misses. It is useful for checking the loading and delegation contract before adding storage:

use Red::AST;
use Red::Driver::Cache;

unit class Red::Driver::Cache::PassThrough does Red::Driver::Cache;

multi method get-from-cache(Red::AST $ast) {
    Nil
}

multi method set-on-cache(Red::AST $ast, @rows) {
    Nil
}

Save it as lib/Red/Driver/Cache/PassThrough.rakumod, then load it with cache 'PassThrough', database('SQLite') using raku -Ilib. Replace the two methods with storage/retrieval once your key and invalidation policy are defined. Do not cache failures as empty results.

String keys and invalidation

Red::Driver::CacheWithStrKey supplies AST-to-string translation and calls get-from-cache(Str $key, :$ast) and set-on-cache(Str $key, @rows, :$ast). See the bundled Memory implementation for these signatures. Unsupported key translations fall back to the underlying driver; the built-in translator is not a complete key scheme for every Red AST.

For a new storage backend, include all result-affecting inputs in the key: projection, tables, filters and their bound values/types, joins, ordering, grouping, limit/offset, connection/database identity and any tenant/session context. Avoid ambiguous delimiters and distinguish null from empty strings. A SQL plan key alone cannot identify result data. Never share entries across connections just because the SQL text matches.

You can compose Red::Driver::CacheInvalidateOnWrite and implement multi method invalidate(Str :$table!, *%columns) to invalidate inserts. Full consistency additionally requires handling updates/deletes, affected tables in joins, external writes and commit/rollback timing. Consider bypassing caching inside transactions. Add capacity/eviction limits, synchronization for concurrent requests and a serialization policy if rows leave the process. Do not let callers mutate the stored row objects through returned results.

The base wrapper delegates only a subset of driver methods. Review the driver contract before promising transparent support for catalogs, migrations, notifications or transaction lifecycle methods. The cache wrapper's schema-reader currently has no implementation.

Verify the behavior

Test repeated reads, different bind values, empty results, unsupported AST bypass, expiry and eviction, insert/update/delete invalidation, rollback and isolation between connections. Compare results with the unwrapped driver, including regex capability checks and errors. For distributed storage, also exercise unavailable storage and serialization failures. The wrapper catches exceptions during SELECT preparation and falls back to the driver; define and test your intended failure policy instead of relying on that catch for every stage of execution.

Red v0.2.17

A Raku ORM

Authors

  • Fernando Correa de Oliveira

License

Artistic-2.0

Dependencies

ConfigurationDBIishUUIDJSON::Fast

Test Dependencies

Provides

  • CX::Red::Bool
  • MetamodelX::Red::Comparate
  • MetamodelX::Red::Describable
  • MetamodelX::Red::Dirtable
  • MetamodelX::Red::GlobalNotifiable
  • MetamodelX::Red::Id
  • MetamodelX::Red::Migration
  • MetamodelX::Red::Model
  • MetamodelX::Red::OnDB
  • MetamodelX::Red::Populatable
  • MetamodelX::Red::PreFetch
  • MetamodelX::Red::Refreshable
  • MetamodelX::Red::Relationship
  • MetamodelX::Red::Specialisable
  • MetamodelX::Red::SubModelHOW
  • MetamodelX::Red::Supply
  • MetamodelX::Red::View
  • MetamodelX::Red::VirtualView
  • Red
  • Red::AST
  • Red::AST::AddForeignKeyOnTable
  • Red::AST::BeginTransaction
  • Red::AST::Between
  • Red::AST::BoundFilter
  • Red::AST::Case
  • Red::AST::Chained
  • Red::AST::ChangeColumn
  • Red::AST::Comment
  • Red::AST::CommitTransaction
  • Red::AST::Constraints
  • Red::AST::CreateColumn
  • Red::AST::CreateIndex
  • Red::AST::CreateTable
  • Red::AST::CreateView
  • Red::AST::DateTimeFuncs
  • Red::AST::Delete
  • Red::AST::Divisible
  • Red::AST::DriverExtension
  • Red::AST::DropColumn
  • Red::AST::Empty
  • Red::AST::Function
  • Red::AST::Generic::Infix
  • Red::AST::Generic::Postfix
  • Red::AST::Generic::Prefix
  • Red::AST::Infix
  • Red::AST::Infixes
  • Red::AST::Insert
  • Red::AST::Intersect
  • Red::AST::IsDefined
  • Red::AST::JsonItem
  • Red::AST::JsonOperation
  • Red::AST::JsonRemoveItem
  • Red::AST::JsonSetItem
  • Red::AST::LastInsertedRow
  • Red::AST::Minus
  • Red::AST::MultiSelect
  • Red::AST::Native
  • Red::AST::Next
  • Red::AST::Operator
  • Red::AST::Optimizer::AND
  • Red::AST::Optimizer::Case
  • Red::AST::Optimizer::OR
  • Red::AST::Ordering
  • Red::AST::ProductSource
  • Red::AST::ProjectionField
  • Red::AST::Reference
  • Red::AST::RegexCapture
  • Red::AST::RegexChars
  • Red::AST::RegexMatch
  • Red::AST::ReleaseSavepoint
  • Red::AST::RollbackToSavepoint
  • Red::AST::RollbackTransaction
  • Red::AST::RowContext
  • Red::AST::RowParameter
  • Red::AST::Savepoint
  • Red::AST::Select
  • Red::AST::StringFuncs
  • Red::AST::TableComment
  • Red::AST::Unary
  • Red::AST::Union
  • Red::AST::Update
  • Red::AST::Value
  • Red::Attr::Column
  • Red::Attr::Index
  • Red::Attr::Query
  • Red::Attr::Relationship
  • Red::Class
  • Red::Cli
  • Red::Cli::Column
  • Red::Cli::Relationship
  • Red::Cli::Table
  • Red::Column
  • Red::ColumnMethods
  • Red::Compiler::Emitter
  • Red::Compiler::RakuAST
  • Red::Compiler::Regex
  • Red::Compiler::Runtime
  • Red::Compiler::Template
  • Red::Config
  • Red::DB
  • Red::Database
  • Red::DefaultResultSeq
  • Red::Do
  • Red::Driver
  • Red::Driver::Cache
  • Red::Driver::Cache::Memory
  • Red::Driver::CacheInvalidateOnWrite
  • Red::Driver::CacheWithStrKey
  • Red::Driver::CommonSQL
  • Red::Driver::Events
  • Red::Driver::Migrations
  • Red::Driver::Mock
  • Red::Driver::SQLite
  • Red::Driver::SQLite::Regex
  • Red::Driver::SQLite::SQLiteMaster
  • Red::Driver::SQLite::SchemaReader
  • Red::Event
  • Red::Fixture
  • Red::Formatter
  • Red::Formatter::ResultSeq
  • Red::Formatter::ResultSeq::Vertical
  • Red::FromRelationship
  • Red::GlobalEvents
  • Red::HiddenFromSQLCommenting
  • Red::Index
  • Red::LockType
  • Red::Migration::Applied
  • Red::Migration::Artifact
  • Red::Migration::Bundle
  • Red::Migration::Column
  • Red::Migration::Configuration
  • Red::Migration::Data
  • Red::Migration::Dependencies
  • Red::Migration::Driver
  • Red::Migration::Environment
  • Red::Migration::File
  • Red::Migration::Import
  • Red::Migration::Logical
  • Red::Migration::Manager
  • Red::Migration::Migration
  • Red::Migration::Planner
  • Red::Migration::Resolution
  • Red::Migration::SQL
  • Red::Migration::Scripts
  • Red::Migration::Snapshot
  • Red::Migration::Storage
  • Red::Migration::Table
  • Red::Migration::Test
  • Red::Model
  • Red::Operators
  • Red::Phaser
  • Red::PrepareCode
  • Red::Projection
  • Red::QueryPlan
  • Red::QueryPlan::Cache
  • Red::Regex
  • Red::Regex::Automaton
  • Red::Regex::Unicode
  • Red::ResultAssociative
  • Red::ResultSeq
  • Red::ResultSeq::Iterator
  • Red::ResultSeq::Supply
  • Red::ResultSeqMethods
  • Red::ResultSeqSeq
  • Red::RowSecurity
  • Red::RowSecurity::Context
  • Red::RowSecurity::DDL
  • Red::RowSecurity::PostgreSQL
  • Red::RowSecurity::Statement
  • Red::RowSecurity::Write
  • Red::Runtime::Configuration
  • Red::Schema
  • Red::SchemaReader
  • Red::Statement
  • Red::Traits
  • Red::Transaction::Guard
  • Red::Type
  • Red::Type::Json
  • Red::Type::Map
  • Red::Utils
  • X::Red::Exceptions
  • X::Red::Migration

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.