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

NAME

IO::Iron::IronCache::Client - IronCache (Online Item-Value Storage) Client.

VERSION

version 0.13

SYNOPSIS

        require IO::Iron::IronCache::Client;
        require IO::Iron::IronCache::Item;
        my $ironcache_client = IO::Iron::IronCache::Client->new();
        # or
        use IO::Iron qw(get_ironcache);
        my $ironcache_client = get_ironcache();

        # Operate with caches.
        my @iron_caches = $ironcache_client->get_caches();
        my $iron_cache = $ironcache_client->create_cache('name' => 'My_Iron_Cache');
        # Or get an existing cache.
        my $iron_cache = $ironcache_client->get_cache('name' => 'My_Iron_Cache');
        my $cache_deleted = $ironcache_client->delete_cache('name' => 'My_Iron_Cache');
        my $info = $ironcache_client->get_info_about_cache('name' => 'My_Iron_Cache');

        # Operate with items.
        my $iron_cache_item_put = IO::Iron::IronCache::Item->new(
                'value' => "10",
                'expires_in' => 60, # Expires in 60 seconds.
                #'replace' => 1, # Only set the item if the item is already in the cache.
                #'add' => 1 # Only set the item if the item is not already in the cache.
                #'cas' => '12345' # Only set the item if there is already an item with matching key and cas.
                );
        my $item_put_ok = $iron_cache->put('key' => 'my_item', 'item' => $iron_cache_item_put);
        my $item_put_new_value = $iron_cache->increment('key' => 'my_item', 'increment' => 15);
        my $iron_cache_item_get = $iron_cache->get('key' => 'my_item');
        my $item_deleted_ok = $iron_cache->delete('key' => 'my_item');
        my $items_cleared_ok = $iron_cache->clear();

DESCRIPTION

IO::Iron::IronCache is a client for the IronCache online key-value store at http://www.iron.io/. IronCache is a cloud based key-value store with a REST API. IO::Iron::IronCache::Client creates a Perl object for interacting with IronCache. All IronCache functions are available.

The class IO::Iron::IronCache::Client instantiates the 'project', IronCache access configuration.

IronCache Key-Value Store

http://www.iron.io/

IronCache is a key-value store online, usable on the principle of "Software as a Service", i.e. SaaS. It is available to Internet connecting applications via its REST interface. Built with distributed cloud applications in mind, it provides on-demand key-value storage, value persistance/expiry as requested and cloud-optimized performance. [see http://www.iron.io/]

Using the IronCache Client Library

IO::Iron::IronCache::Client is a normal Perl package meant to be used as an object.

        require IO::Iron::IronCache::Client;
        my $iron_cache_client = IO::Iron::IronCache::Client->new();

Please see IO::Iron for further parameters and general usage.

After creating the client, the client can create a new cache (storage), get or delete an old one or get all the existing caches within the same project.

The client has all the methods which interact with the caches; the cache (object of class IO::Iron::IronCache::Cache) has methods which involve items inside the cache.

When failed to do the requested action, the methods return an exception using Perl package Exception::Class. Calling program should trap these with e.g. Perl package Try::Tiny.

        # Create the cache client.
        require IO::Iron::IronCache::Client;
        my $ironcache_client = IO::Iron::IronCache::Client->new();
        # Or
        $ironcache_client = IO::Iron::IronCache::Client->new(
                config => 'iron_cache.json
                );

        # Operate with caches.
        # Get all the existing caches as objects of
        # class IO::Iron::IronCache::Cache.
        my @iron_caches = $ironcache_client->get_caches();

        # Create a new cache object by its name.
        # Returns object of class IO::Iron::IronCache::Cache.
        my $iron_cache = $ironcache_client->create_cache('name' => 'My_Iron_Cache');
        # Or get an existing cache.
        $iron_cache = $ironcache_client->get_cache('name' => 'My_Iron_Cache');

        # Delete a cache by its name. Return 1 for success.
        my $cache_deleted = $ironcache_client->delete_cache('name' => 'My_Iron_Cache');

        # Get info about a cache.
        my $info_hash = $ironcache_client->get_info_about_cache('name' => 'My_Iron_Cache');

        # Operate with items.
        # Create an item.
        my $iron_cache_item_put = IO::Iron::IronCache::Item->new(
                'value' => "10",
                'expires_in' => 60, # Expires in 60 seconds.
                #'replace' => 1, # Only set the item if the item is already in the cache.
                #'add' => 1, # Only set the item if the item is not already in the cache.
                #'cas' => '12345', # Only set the item if there is already an item with matching key and cas.
                );
        my $item_put = $iron_cache->put('key' => 'my_item_key', 'item' => $iron_cache_item_put);
        my $item_put_new_value = $iron_cache->increment('key' => 'my_item_key', 'increment' => 15);
        my $iron_cache_item_get = $iron_cache->get('key' => 'my_item_key');
        my $item_deleted = $iron_cache->delete('key' => 'my_item_key');

        # Empty the cache (delete all items inside). Return 1 for success.
        my $items_cleared_ok = $iron_cache->clear();

An IO::Iron::IronCache::Cache object gives access to a single cache. With it you can do all the normal things one would with a key-value store.

Items are objects of the class IO::Iron::IronCache::Item. It contains the following attributes:

- value, Free text. If you want to put an object or a hash here, it needs to be serialized first; use e.g. JSON, Storable or YAML to stringify it. Then give the resulting string here.
- expires_in, How long in seconds to keep the item in the cache before it is deleted. By default, items do not expire. Maximum is 2,592,000 seconds (30 days).
- replace, If set to true, only set the item if the item is already in the cache. If the item is not in the cache, do not create it.
- add, If set to true, only set the item if the item is not already in the cache. If the item is in the cache, do not overwrite it.
- cas: If set, the new item will only be placed in the cache if there is an existing item with a matching key and cas value. An item's cas value is automatically generated and is included when the item is retrieved.

-item - N.B. The item key is not stored in the object.

Cas value changes every time the item value is updated to cache. It can be used to verify that the value has not been changed since the last get operation.

        $iron_cache_item_key = 'my_item_key';
        my $iron_cache_item_put_1 = IO::Iron::IronCache::Item->new(
                'value' => "10",
                'expires_in' => 60, # Expires in 60 seconds.
                'replace' => 1,
                );
        # Or
        my $iron_cache_item_put_2 = IO::Iron::IronCache::Item->new(
                'value' => "10",
                'expires_in' => 60, # Expires in 60 seconds.
                'add' => 1,
                );

IO::Iron::IronCache::Cache objects are created by the client (object of IO::Iron::IronCache::Client) or they can be created by the user. If an item is put to a cache which doesn't exist yet, IronCache creates a new cache automatically.

While it is possible to create a cache object from IO::Iron::IronCache::Cache, user should not normally do this. When the cache object is created by the Client, it gets the Client's REST connection parameters. Otherwise these will need to be set manually.

With an IO::Iron::IronCache::Cache object you can put items to the cache, or get existing items from it.

Get cache id. Not really needed for anything. Just internal reference for Iron Cache.

        my $cache_id = $iron_cache->id();

Get cache name.

        my $cache_name = $iron_cache->name();

Put an item into the cache. Returns 1 if successful.

        my $item_put = $iron_cache->put('key' => $iron_cache_item_key, 'item' => $iron_cache_item_put);

If the item is an integer value, you can simply increment it by another value. If the value is negative, the value in the cache will be decreased. Returns the new value.

        my $item_put_new_value = $iron_cache->increment('key' => $iron_cache_item_key, 'increment' => 15);

Get an item from the cache by its name. Returns an object of the class IO::Iron::IronCache::Item if successful.

        my $iron_cache_item_get = $iron_cache->get('key' => $iron_cache_item_key);

Delete an item in the cache by its name. Returns 1 if successful.

        my $item_deleted_ok = $iron_cache->delete('key' => $iron_cache_item_key);

Clear the cache (delete all items inside). Return 1 for success.

        my $items_cleared_ok = $iron_cache->clear();

Exceptions

Please see IO::Iron for documentation on exceptions.

Policies

Please see IO::Iron for documentation on policies (limitations to names).

REQUIREMENTS

See IO::Iron for requirements.

SUBROUTINES/METHODS

new

Creator function.

get_caches

Return objects of class IO::Iron::IronCache::Cache representing all the caches within this project.

Params: [None]
Return: List of IO::Iron::IronCache::Cache objects.

get_info_about_cache

Params: cache name.
Return: Ref to a hash containing info about cache. Contains at least items id, project_id, name and size.

get_cache

Return a IO::Iron::IronCache::Cache object representing a particular key-value cache. The cache object is linked to the creating IO::Iron::IronCache::Client object.

Params: cache name. Cache must exist. If not, fails with an exception.
Return: IO::Iron::IronCache::Cache object.
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

create_cache

Return a IO::Iron::IronCache::Cache object representing a particular message cache. This call doesn't actually access IronCache API because, if an item is put to a cache which doesn't exist yet, IronCache creates a new cache automatically. create_cache only creates a new IO::Iron::IronCache::Cache object which is linked to its creator IO::Iron::IronCache::Client object.

Params: cache name.
Return: IO::Iron::IronCache::Cache object.

delete_cache

Delete an IronCache cache.

Params: cache name. Cache must exist. If not, fails with an exception.
Return: 1 == success.
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

AUTHOR

Mikko Koivunalho <mikko.koivunalho@iki.fi>

BUGS

Please report any bugs or feature requests to bug-io-iron@rt.cpan.org or through the web interface at: http://rt.cpan.org/Public/Dist/Display.html?Name=IO-Iron

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Mikko Koivunalho.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

The full text of the license can be found in the LICENSE file included with this distribution.