Key-Value

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Key-Value Operations

The CacheInterface provides methods to work with the key and the value of the cache using Key-Value operations - put, get, put all, get all, replace and others. This example shows how to do that:

use Apache\Ignite\Client;
use Apache\Ignite\ClientConfiguration;
use Apache\Ignite\Type\ObjectType;
use Apache\Ignite\Cache\CacheEntry;
use Apache\Ignite\Exception\ClientException;

function performCacheKeyValueOperations(): void
{
    $client = new Client();
    try {
        $client->connect(new ClientConfiguration('127.0.0.1:10800'));
        $cache = $client->getOrCreateCache('myCache')->
            setKeyType(ObjectType::INTEGER);
        
        // put and get value
        $cache->put(1, 'abc');
        $value = $cache->get(1);

        // put and get multiple values using putAll()/getAll() methods
        $cache->putAll([new CacheEntry(2, 'value2'), new CacheEntry(3, 'value3')]);
        $values = $cache->getAll([1, 2, 3]);

        // removes all entries from the cache
        $cache->clear();
    } catch (ClientException $e) {
        echo($e->getMessage());
    } finally {
        $client->disconnect();
    }
}

performCacheKeyValueOperations();
📘

PHP example files

PHP thin client contains fully workable examples to demonstrate the behavior of the client.

Scan Query

PHP client supports Ignite scan queries. A query method returns a cursor object with the standard PHP Iterator interface which allows to iterate over the set with the query results lazily, one by one. Additionally, the cursor has methods to get the whole results at once.

First, define the query by creating and configuring an instance of the ScanQuery class.

Then, pass the ScanQuery instance to the query method of the CacheInterface.

Finally, use the returned object with the CursorInterface to iterate over or get all cache entries returned by the query.

use Apache\Ignite\Client;
use Apache\Ignite\ClientConfiguration;
use Apache\Ignite\Type\ObjectType;
use Apache\Ignite\Cache\CacheEntry;
use Apache\Ignite\Query\ScanQuery;
use Apache\Ignite\Exception\ClientException;

function performScanQuery(): void
{
    $client = new Client();
    try {
        $client->connect(new ClientConfiguration('127.0.0.1:10800'));
        $cache = $client->getOrCreateCache('myCache')->
            setKeyType(ObjectType::INTEGER);
        
        // put multiple values using putAll()
        $cache->putAll([
            new CacheEntry(1, 'value1'),
            new CacheEntry(2, 'value2'), 
            new CacheEntry(3, 'value3')]);
        
        // create and configure scan query
        $scanQuery = (new ScanQuery())->
            setPageSize(1);
        // obtain scan query cursor
        $cursor = $cache->query($scanQuery);
        // getAll cache entries returned by the scan query
        foreach ($cursor->getAll() as $cacheEntry) {
            echo($cacheEntry->getValue() . PHP_EOL);
        }
        
        $client->destroyCache('myCache');
    } catch (ClientException $e) {
        echo($e->getMessage());
    } finally {
        $client->disconnect();
    }
}

performScanQuery();