Mikko Koivunalho > IO-Iron > IO::Iron::IronCache::Client

Download:
IO-Iron-0.07.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.07   Source   Latest Release: IO-Iron-0.07_03

NAME ^

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

VERSION ^

Version 0.07

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();

REQUIREMENTS ^

See IO::Iron for requirements.

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. Will be JSONized. If you need an object serialized, don't use JSON. Use e.g. YAML. 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

A REST call to Iron service may fail for several reason. All failures generate an exception using the Exception::Class package. Class IronHTTPCallException contains the field status_code, response_message and error. Error is formatted as such: IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>.

        use Try::Tiny;
        use Scalar::Util qw{blessed};
        try {
                my $queried_iron_cache_01 = $iron_cache_client->get_cache('name' => 'unique_cache_name_01');
        }
        catch {
                die $_ unless blessed $_ && $_->can('rethrow');
                if ( $_->isa('IronHTTPCallException') ) {
                        if ($_->status_code == 404) {
                                print "Bad things! Can not just find the catch in this!\n";
                        }
                }
                else {
                        $_->rethrow; # Push the error upwards.
                }
        };

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: a hash containing info about cache.

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 the creating 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 at iki.fi>

BUGS ^

Please report any bugs or feature requests to bug-io-iron at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Iron. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc IO::Iron::IronCache::Client

You can also look for information at:

LICENSE AND COPYRIGHT ^

Copyright 2013 Mikko Koivunalho.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax highlighting: