PHPackages                             mauretto78/in-memory-list - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. mauretto78/in-memory-list

ActiveLibrary[Caching](/categories/caching)

mauretto78/in-memory-list
=========================

In Memory List

2.2.10(6y ago)34952MITPHPPHP ^5.6 || ^7.0

Since May 16Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mauretto78/in-memory-list)[ Packagist](https://packagist.org/packages/mauretto78/in-memory-list)[ Docs](https://github.com/mauretto78/in-memory-list)[ RSS](/packages/mauretto78-in-memory-list/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (45)Used By (0)

In-memory List
==============

[](#in-memory-list)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ac155a60317e884621113ac3784ee4be9960abc8956b65f4f83d0a435625efdd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6175726574746f37382f696e2d6d656d6f72792d6c6973742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mauretto78/in-memory-list/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/1b11abab35bdbe74f2c62db285a3316ad7160d8adae2aa065df2acad89ece628/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f35626630383661662d653435652d343866362d393864642d6237643565613037343133302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/5bf086af-e45e-48f6-98dd-b7d5ea074130)[![Build Status](https://camo.githubusercontent.com/6bc73ee6faca8c0865ce570ef9be426eb5ce18b2a22004fdedfb49c605f9b5d5/68747470733a2f2f7472617669732d63692e6f72672f6d6175726574746f37382f696e2d6d656d6f72792d6c6973742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mauretto78/in-memory-list)[![Codacy Badge](https://camo.githubusercontent.com/084540649690a6332e22ab2bdd3e4d014c6645abfaa1cdd39b593fa6a3c658ea/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3834643833323234393663623461313162646330636130316134323731623532)](https://www.codacy.com/app/mauretto78/in-memory-list?utm_source=github.com&utm_medium=referral&utm_content=mauretto78/in-memory-list&utm_campaign=Badge_Grade)![license](https://camo.githubusercontent.com/5d5234d7e18955e07fcc34b7f5facb5bb4510fb086293bd57225dc53a11bc55b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6175726574746f37382f696e2d6d656d6f72792d6c6973742e737667)![Packagist](https://camo.githubusercontent.com/9162246dae7d9db80e32b8928beabcb0da83eb6156ef1a38fc3f10725241eeda/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6175726574746f37382f696e2d6d656d6f72792d6c6973742e737667)

**In-memory List** easily allows you to create and save your lists in memory.

If you are looking for a caching system for your lists this library is suitable for you.

Grab your lists from your API, your database or whatever you want and store them in memory: then, you can quickly retrieve your lists from cache, sorting and performing queries on them.

This package uses:

- [Apcu](http://php.net/manual/en/book.apcu.php)
- [Memcached](https://memcached.org/)
- [Pdo](http://php.net/manual/en/book.pdo.php)
- [Redis](https://redis.io/)

Basic Usage
-----------

[](#basic-usage)

To create and store in memory you list do the following:

```
use InMemoryList\Application\Client;

$array = [
    ...
]

$client = new Client();
$collection = $client->create($array);

foreach ($collection as $element){
    // ...
}
```

Drivers
-------

[](#drivers)

Avaliable drivers:

- `apcu`
- `memcached`
- `pdo`
- `redis` (default driver)

```
use InMemoryList\Application\Client;

// Apcu, no configuration is needed
$client = new Client('apcu');
// ..
```

```
use InMemoryList\Application\Client;

// Memcached, you can pass one or more servers
$memcached_parameters = [
    [
        'host' => 'localhost',
        'port' => 11211
    ],
    [
        'host' => 'localhost',
        'port' => 11222
    ],
    // etc..
];

$client = new Client('memcached', $memcached_parameters);
// ..
```

```
use InMemoryList\Application\Client;

// Pdo
$pdo_parameters = [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'database' => 'in-memory-list'
    'port' => '3306'
];

$client = new Client('pdo', $pdo_parameters);
// ..
```

```
use InMemoryList\Application\Client;

// you have to use arrays
// you can't use URI string like 'tcp://10.0.0.1:6379'
// please refer to PRedis library documentation
$redis_parameters = [
    'scheme' => 'tcp',
    'host' => '127.0.0.1',
    'port' => 6379,
    'options' => [
        'profile' => '3.0',
    ],
];

$client = new Client('redis', $redis_parameters);
// ..
```

Refer to [official page](https://github.com/nrk/predis) for more details on PRedis connection.

Parameters
----------

[](#parameters)

When use `create` method to a generate a list, you can provide to it a parameters array. The allowed keys are:

- `uuid` - uuid of list
- `element-uuid` - uuid for the list elements
- `headers` - headers array for the list
- `chunk-size` - the chunks size in which the array will be splitted (integer) \*\*
- `ttl` - time to live of the list (in seconds) \*\*

\*\* = NOT AVALIABLE WITH PDO DRIVER

### uuid

[](#uuid)

You can assign an uuid to your list (instead, a [uuid](https://github.com/ramsey/uuid) will be generated):

```
use InMemoryList\Application\Client;

$array = [
    ...
]

$client = new Client();
$client->create($array, [
    'uuid' => 'simple-array'
]);

// And now you can retrive the list:
$simpleArray = $client->getRepository()->findListByUuid('simple-array');

//..
```

### headers

[](#headers)

You can set a headers array to your list:

```
use InMemoryList\Application\Client;

$array = [
    ...
]

$headers = [
    'expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
    'hash' => 'ec457d0a974c48d5685a7efa03d137dc8bbde7e3'
];

$client = new Client();
$collection = $client->create($array, [
    'uuid' => 'simple-array',
    'headers' => $headers
]);

// get headers
var_dump($client->getRepository()->getHeaders('simple-array'));

// ...
```

### element-uuid

[](#element-uuid)

You can assign an uuid to list elemens (instead, a [uuid](https://github.com/ramsey/uuid) will be generated). Consider this array:

```
$simpleArray = [
    [
        "userId" => 1,
        "id" => 1,
        "title" => "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body" =>  "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    ],
    ...
]
```

Maybe you would use `id` key as uuid in your list:

```
use InMemoryList\Application\Client;

$client = new Client();
$collection = $client->create($simpleArray, [
    'uuid' => 'simple-array',
    'element-uuid' => 'id'
]);

// now to retrieve a single element, you can simply do:
$itemWithId1 = $collection[1];
```

### chunk-size

[](#chunk-size)

You can specify the number of elements of each chunk in which the original array will be splitted. The default value is `1000`.

```
use InMemoryList\Application\Client;

$client = new Client();
$collection = $client->create($array, [
    'uuid' => 'simple-array',
    'element-uuid' => 'id',
    'chunk-size' => 1500
]);

// ..
```

PLEASE NOTE THAT `chunk-size` IS NOT AVALIABLE WITH PDO DRIVER

### ttl

[](#ttl)

You can specify a ttl (in seconds) for your lists:

```
use InMemoryList\Application\Client;

$client = new Client();
$collection = $client->create($array, [
    'uuid' => 'simple-array',
    'element-uuid' => 'id',
    'ttl' => 3600
]);

// ..
```

PLEASE NOTE THAT `ttl` IS NOT AVALIABLE WITH PDO DRIVER

Delete an element
-----------------

[](#delete-an-element)

To delete an element in you list do this:

```
// ..
$client->getRepository()->deleteElement(
    $listUuid,
    $elementUuid,
);
```

Push an element
---------------

[](#push-an-element)

To push an element in you list, use `pushElement` function. You must provide the list uuid, the element uuid and element data (data must be consistent - see Validation). Look at this example:

```
// ..
$client->pushElement(
    'fake-list-uuid',
    5001,
    [
        'id' => 5001,
        'name' => 'Name 5001',
        'email' => 'Email 5001',
    ]
);
```

Update an element
-----------------

[](#update-an-element)

To update an element in you list, use `updateElement` function. You must provide the list uuid, the element uuid and element data (data must be consistent - see Validation). Look at this example:

```
// ..
$client->getRepository()->updateElement(
    'list-to-update',
    4325,
    [
        'id' => 4325,
        'title' => 'New Title',
        // ..
    ]
);
```

Ttl
---

[](#ttl-1)

You can update ttl of a persisted list with `updateTtl` method, and retrive the ttl with `getTtl` function:

```
// ...
$client->getRepository()->updateTtl(
    'your-list-uuid',
    3600 // ttl in seconds
);

// get Ttl of the list
$client->getRepository()->getTtl('your-list-uuid'); // 3600
```

PLEASE NOTE THAT `ttl` IS NOT AVALIABLE WITH PDO DRIVER

Validation (Data consistency)
-----------------------------

[](#validation-data-consistency)

Please note that your data **must be consistent**:

```
// simple string list
$stringArray = [
    'Lorem Ipsum',
    'Ipse Dixit',
    'Dolor facium',
];

$collection = $client->create($stringArray, [
    'uuid' => 'string-array',
    'ttl' => 3600
]);

// array list, you must provide elements with consistent structure
$listArray[] = [
    'id' => 1,
    'title' => 'Lorem Ipsum',
];
$listArray[] = [
    'id' => 2,
    'title' => 'Ipse Dixit',
];
$listArray[] = [
    'id' => 3,
    'title' => 'Dolor facium',
];

$collection = $client->create($listArray, [
    'uuid' => 'simple-array',
    'element-uuid' => 'id',
    'ttl' => 3600
]);

// entity list, the objects must have the same properties
$entityArray[] = new User(1, 'Mauro');
$entityArray[] = new User(2, 'Cristina');
$entityArray[] = new User(3, 'Lilli');

$collection = $client->create($entityArray, [
    'uuid' => 'entity-array',
    'element-uuid' => 'id',
    'ttl' => 3600
]);
```

Instead, a `ListElementNotConsistentException` will be thrown. Example:

```
// ListElementNotConsistentException will be thrown
$listArray[] = [
    'id' => 1,
    'title' => 'Lorem Ipsum',
];
$listArray[] = [
    'id' => 2,
    'non-consistent-key' => 'Ipse Dixit',
];
$listArray[] = [
    'id' => 3,
    'title' => 'Dolor facium',
];

$collection = $client->create($listArray, [
    'uuid' => 'simple-array',
    'element-uuid' => 'id',
    'ttl' => 3600
]);
```

Sorting and Quering
-------------------

[](#sorting-and-quering)

You can perform queries on your list. This library uses [Array Query](https://github.com/mauretto78/array-query), please refer to it for the official documentation.

```
use ArrayQuery\QueryBuilder;

// ..

$list = $client->getRepository()->findListByUuid('simple-array');

$qb = QueryBuilder::create($list);
$qb
    ->addCriterion('id', '3', '>')
    ->sortedBy('id', 'DESC');

// get results
foreach ($qb->getResults() as $element){
    // ...
}
```

Commands
--------

[](#commands)

If you have an application which uses [Symfony Console](https://github.com/symfony/console), you have some commands avaliable:

- `iml:cache:flush` to flush the cache
- `iml:cache:index [] []` to get full index of items stored in cache
- `iml:cache:schema:create` Create database schema (only for PDO driver)
- `iml:cache:schema:destroy` Destroys database schema (only for PDO driver)
- `iml:cache:statistics` to get cache statistics

You can register the commands in your app, consider this example:

```
#!/usr/bin/env php
