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

NAME

Starch::Manual - Implementation independent persistent statefulness.

DESCRIPTION

Welcome to Starch, an implementation agnostic state persistence manager. Starch exposes stateful buckets of arbitrary information which are tracked using some implementation specific technology, such as HTTP cookies. While this module's uses extend far beyond simply making HTTP requests stateful, this documentation uses the HTTP paradigm to explain how Starch works.

Typically you'll be integrating Starch with a web framework such as Catalyst (see "INTEGRATIONS" for other frameworks). Whichever integration you are using it will likely pass-through arguments to the underlying Starch::Manager object and provide a thin layer over the Starch::State objects. This documentation will be using the core Starch classes for examples. Your integration layer may change, or completely replace, the interface documented here.

Starch has several design philosophies:

  • Is as fast as possible by limiting method calls, implementing lazy-loading wherever it can be done, and using libraries which exhibit run-time efficiencies which beat out their competitors.

  • Reduces data store reads and writes to just the most essential.

  • Is independent from any particular framework (such as Catalyst or Plack).

  • Provides a straight-forward and powerful mechanism for customizing just about any part of Starch via stores and plugin bundles.

  • Is easy to understand due to everything being well documented, hyper-linked, and containing thorough examples and tests.

There are many "ALTERNATIVES" to Starch to choose from, all of which Starch was inspired from and may be a superior choice to.

BASIC USAGE

When setting up you need to, at a minimum, define a store:

    my $starch = Starch->new(
        store => { class=>'::Memory' },
    );

A store is a hash ref of arguments which are used for constructing the store object. A store object implements a very simple interface for setting, getting, and removing state data. Beyond defining the store you will not be interacting with it as the Starch::State objects do all the store interaction for you.

When defining the store you must specify at least the class argument which determines the store class to use. This class name can be relative to Starch::Store so that if you specify ::Memory, as in the example above, it will be resolved to the Starch::Store::Memory class. An absolute store class name may be used without the leading :: if you have a custom store in a different namespace.

Calling the new method on the Starch package actually returns a Starch::Manager object, so refer to its documentation for details on what arguments you can pass.

Now that you have the $starch object you can create a state object:

    my $state = $starch->state();

This creates a new Starch::State object which you can then interact with:

    $state->data->{some_key} = 'some_value';

The "data" in Starch::State attribute is a writeable hash ref which can contain any data you want. This is the data which will be stored by, and retrieved from, the store. Once you're done making changes to the data, call save:

    $state->save();

This stores the state data in the store.

Each state gets assigned a state ID automatically which can be used to retrieve the state data at a later time. The state ID is a randomly generated SHA-1 hex digest.

    my $id = $state->id();

To retrieve a previously saved state pass the state ID to the "state" in Starch::Manager method:

    my $state = $starch->state( $id );

And now you can access the data you previously saved:

    print $state->data->{some_key}; # "some_value"

Your framework integration, such as Catalyst::Plugin::Starch, will wrap up and hide away most of these details from you, but it's still good to know what is happening behind the scenes.

EXPIRATION

Expiration can be specified globally, when instantiating the Starch::Manager object, per-state, and per-store. The expires value has various properties and behaviors that are important to understand:

  • The expires field is always specified as the number of seconds before the state will expire.

  • The Starch::Manager class accepts an expires argument which is used as the default expires for new state objects and used as the expiration for cookies via Starch::Plugin::CookieArgs.

  • States have a expires argument which defaults to the value of the global expires set in the Starch::Manager object. Each state can then have their individual expire extended or reduced via the "set_expires" in Starch::State method.

  • Stores may have a max_expires argument passed to them. If the per-state expires is larger than the store's max_expires then the state's expires will be replaced with the store's max_expires when writing the data to the store.

LOGGING

Starch has built-in logging facilities via Log::Any. By default, nothing is logged. Various plugins and stores do use logging, such as the LogStoreExceptions plugin.

If you do not set up a log adapter then these log messages will disappear into the void. Read the Log::Any documentation for instructions on configuring an adapter to capture the log output.

The Starch::Plugin::Trace plugin adds a bunch of additional logging output useful for development.

METHOD PROXIES

The Starch manager (Starch::Manager) and stores support method proxies out of the box for all arguments passed to them. A method proxy is an array ref which is lightly inspired by JSON references. This array ref must have the string &proxy as the first value, a package name as the second value, a method name as the third value, and any number of arguments to pass to the method after that:

    [ '&proxy', $package, $method, @args ]

Method proxies are really useful when you are configuring Starch from static configuration where you cannot dynamically pass a value from Perl.

An example from Starch::Store::CHI illustrates how this works:

    my $starch = Starch->new(
        store => {
            class => '::CHI',
            chi => ['&proxy', 'My::CHI::Builder', 'get_chi'],
        },
    );

This will cause My::CHI::Builder to be loaded, if it hasn't already, and then My::CHI::Builder->get_chi() will be called and the return value used as the value for the chi argument.

Another practical example of using this is with DBI where normally you would end up making a separate connection to your database for states. If your state database is the same database as you use for other things it may make sense to use the same $dbh for both so that you do not double the number of connections you are making to your database.

Method proxies can be used with the manager and store objects at any point in their arguments. For example, if you have Perl code that builds the Starch configuration from the ground up you could:

    my $starch = Starch->new(
        [ '&proxy', 'My::Starch::Config', 'get_config' ],
    );

Which will call get_config on the My::Starch::Config package and use its return value as the arguments for instantiating the Starch object.

PERFORMANCE

On a decently-specced developer laptop Starch adds, at most, one half of one millisecond to every HTTP request. This non-scientific benchmark was done using the Memory store and a contrived example of the typical use of a state as the backend for an HTTP session.

Starch is meant to be as fast as possible while still being flexible. Due to Starch avoiding dependencies, and having zero non-core XS dependencies, there are still same areas which could be slightly faster. At this time there is one plugin which will provide a small performance gain Starch::Plugin::Sereal. Even then, the gain using this plugin will be in the order of a fraction of a millisecond per each HTTP request.

Starch has gone through the wringer performance wise and there just are not many performance gains to be eked out of Starch. Instead you'll likely find that your time in Starch is primarily spent in your store. So, when setting up Starch, picking a store is the most important decision you can make performance wise.

STORES

These stores are included with the Starch distribution:

These stores are distributed separately on CPAN:

More third-party stores can be found on meta::cpan.

PLUGINS

Plugins alter the behavior of the manager (Starch::Manager), state (Starch:State), and store (Starch::Store) objects. To use a plugin pass the plugins argument when creating your Starch object:

    my $starch = Starch->new(
        plugins => ['::Trace'],
        store => { ... },
        ...,
    );

These plugins are included with the Starch distribution:

These plugins are distributed separately on CPAN:

More third-party plugins can be found on meta::cpan.

INTEGRATIONS

The following Starch integrations are available:

Integrations for Plack, Dancer2, Mojolicious, etc will be developed as needed by the people that need them.

EXTENDING

Starch can be extended by plugins and stores. See Starch::Extending for instructions on writing your own.

ALTERNATIVES

DEPENDENCIES

The Starch distribution is shipped with minimal dependencies and with no non-core XS requirements. This is important for many people.

SUPPORT

Please submit bugs and feature requests to the Starch GitHub issue tracker:

https://github.com/bluefeet/Starch/issues