PHPackages                             infira/cachly - 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. infira/cachly

ActiveLibrary[Caching](/categories/caching)

infira/cachly
=============

Quick shortcuts to https://github.com/symfony/cache caching solution

v1.4(1mo ago)0489MITPHPPHP &gt;=8.1

Since Oct 1Pushed 2y ago1 watchersCompare

[ Source](https://github.com/infira/Cachly)[ Packagist](https://packagist.org/packages/infira/cachly)[ RSS](/packages/infira-cachly/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (7)Used By (0)

Cachly
======

[](#cachly)

Quick shortcuts to [Symfony](https://github.com/symfony/cache) caching solution
It is released under the [MIT licence](https://github.com/infira/Cachly/blob/master/LICENCE.md). Drivers:

- Database (PDO)
- FileSystem
- [Memcached](https://www.php.net/manual/en/book.memcached.php)
- [Redis](https://github.com/phpredis/phpredis)
- Session

You can comment, offer changes, otherwise contribute [here on GitHub](https://github.com/infira/Cachly/issues), or ask questions to

Table of contents
=================

[](#table-of-contents)

1. \[Installing/Configuring\]
    - [Basic configure](#basic-configure)
        - [Configure PDO/database adapter adapter](#configure-pdodatabase-adapter-adapter)
        - [Configure session adapter adapter](#configure-session-adapter-adapter)
        - [Configure filesystem adapter](#configure-filesystem-adapter)
        - [Configure memory adapter](#configure-memory-adapter)
            - [Configure memcached adapter](#set-default-memory-adapter)
            - [Configure redis adapter](#configure-redis-adapter)
            - [Set default memory adapter ](#set-default-memory-adapter)
        - [Configure your own adapter (basic)](#configure-your-own-adapter-basic)
        - [Configure your own adapter (with shortcuts)](#configure-your-own-adapter-with-shortcuts)
2. [Examples](#examples)
    - [Basic](#default-adapter-with-default-instance)
    - [New instance](#using-a-new-instance-for-default-adapter)
    - [Adapter instance shortcuts](#adapter-instance-shortcuts)

Installing/Configuring
======================

[](#installingconfiguring)

Basic configure
---------------

[](#basic-configure)

```
$ composer require infira/cachly
```

```
require_once 'vendor/autoload.php';

use Infira\Cachly\Cachly;

Cachly::configure([
    'defaultAdapter' => Cachly::SESS,
    'memAdapter' => Cachly::REDIS,
    'defaultInstanceName' => 'cachly-production',
]);
```

**Options**

- `defaultAdapter` - adapter which will be used when accessing Cachly::$instanceMethod(...)
- `memAdapter` - adapter which will be used when accessing Cachly::mem()-&gt;$instanceMethod
- `defaultInstanceName` - default namespace
- `Cachly::setPropertyInstances` - for accessing Cachly::$sess instance

Configure PDO/database adapter adapter
--------------------------------------

[](#configure-pdodatabase-adapter-adapter)

```
Cachly::configureDbAdapter([
    'dsn' => 'mysql:host=mysql.docker;dbname=cachly',
    'table' => 'cachly_cache2',
    'user' => 'root',
    'password' => 'parool',
]);
```

Configure session adapter adapter
---------------------------------

[](#configure-session-adapter-adapter)

```
Cachly::configureSessionAdapter(static function($namespace) {
    Session::init();

    return new \Infira\Cachly\Adapter\SessionAdapter($namespace);
});
```

Configure filesystem adapter
----------------------------

[](#configure-filesystem-adapter)

```
Cachly::configureFileSystemAdapter([
    'directory' => __DIR__ . '/fileCache'
]);
```

Configure memory adapter
------------------------

[](#configure-memory-adapter)

### Configure memcached adapter

[](#configure-memcached-adapter)

```
Cachly::configureMemcachedAdapter([
    'host' => 'memcached://my.server.com:11211',
    'options' => [
        //... see https://symfony.com/doc/current/components/cache/adapters/memcached_adapter.html#configure-the-options
    ]
]);
```

### Configure redis adapter

[](#configure-redis-adapter)

```
Cachly::configureRedisAdapter([
    'host' => 'redis://localhost:6379',
    'options' => [
        //... see https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html#configure-the-options
    ]
]);
```

### Set default memory adapter

[](#set-default-memory-adapter)

```
Cachly::configure([
     //...
    'memAdapter' => Cachly::REDIS
]);
Cachly::setPropertyInstances([
    Cachly::MEM => static fn() => Cachly::mem(),
]);
//now you can access
Cachly::$sess->put(...)
//or
Cachly::sess('new instance')->put(...)
```

Configure your own adapter (basic)
----------------------------------

[](#configure-your-own-adapter-basic)

@see  for more information

```
Cachly::configureAdapter('myAdapter', function (string $namespace) {
    return MyAwesomeAdapter($namespace);
});
//accessing instance
Cachly::instance('cachly','myAdapter')->set('myKey','myValue');
```

Configure your own adapter (with shortcuts)
-------------------------------------------

[](#configure-your-own-adapter-with-shortcuts)

```
class MyCachly extends Cachly
{
    public static \Infira\Cachly\CacheInstance $myAdapter;

    public static function configure(array $options = []): void
    {
        parent::configure($options);
        Cachly::configureAdapter(
            'myAdapter', function(string $namespace) {
            return MyAwesomeAdapter($namespace);
        });
        static::$myAdapter = static::myAdapterInstance();
    }

    public static function myAdapterInstance(string $namespace = null): \Infira\Cachly\CacheInstance
    {
        return static::instance($namespace, 'myAdapter')
    }
}
//now you can use
MyCachly::configure();
MyCachly::$myAdapter->set('myKey', 'value');
//or creating new instance
MyCachly::myAdapterInstance('new instance')->set('myKey', 'value');
```

### Shortcuts

[](#shortcuts)

```
Cachly::setPropertyInstances([
    Cachly::SESS => static fn() => Cachly::sess(),
    Cachly::DB => static fn() => Cachly::db(),
    Cachly::FILE => static fn() => Cachly::file(),
]);
$Cachly::$sess->put(...);
```

Examples
========

[](#examples)

Choose your favorite

```
use Infira\Cachly\CacheItem;

if (!Cachly::has('myKey')) {
    Cachly::put('myKey', 'my Value', '+1 day'); //save value immediately and will expire after one day
}

Cachly::get('key','defaultValue'); //defaultValue
Cachly::get('key'); //null
Cachly::put('key','stored value'); //CacheItem
Cachly::get('key','defaultValue'); //stored value
Cachly::get('key'); //stored value

//run callback when value does not exist
Cachly::get('compute-key','defaultValue'); //defaultValue
Cachly::get('compute-key'); //null
Cachly::get('compute-key', function (CacheItem $item) {
    $item->expiresAfter(3600);

    // do computations
    $computedValue = 'foobar';

    return $computedValue;
});
Cachly::get('compute-key','defaultValue'); //foobar
Cachly::get('compute-key'); //foobar

$item = Cachly::set('array', ['init value']);
$item[] = 'value2';
$item['key'] = 'value3';
Cachly::get('array'); //foobar
```

### Using method arguments as key

[](#using-method-arguments-as-key)

Use automated hashing from any value to compute value.

```
function filterItems(DateTimeInterface $date, array $filters): mixed
{
    //add as many variables as you want as long last variable is callable
    return Cachly::once('getDataFromDataBase', $date, $filters, function (CacheItem $item) use ($date, $filters) {
        $item->expiresAfter(3600);

        return db()->where('date', $date)->where($filters);
    });
}
```

### Defer

[](#defer)

```
$item = Cachly::set('key','value1')->expire('tomorrow');
$item->set('value2')
Cachly::get('key'); //value2
Cachly::setMany(['key1'=> 'value1','key2' => 'value2']);
Cachly::commit(); //save all deferred items
Cachly::get('key'); //value2
```

### Save, commit, autoSave

[](#save-commit-autosave)

```
$item = Cachly::set('key2','value1')->expire('tomorrow')->save(); //saves value to database
$item->save(); //will not save because nothing has changed since last save
$item->commit(); //will save value without checking changes
$item->set('newValue')->save(); //will save
$item->set('newValue')->save(); //do nothing
$item->expire(5)->save(); //will save

$autoSave = Cachly::set('key2','value1')->autoSave(true); //tries save after every change
$autoSave->set('new value'); //will call save()
$autoSave->expire('tomorrow'); //will also call save
```

### Sub instances

[](#sub-instances)

```
$sub = Cachly::sub('myCollectionName');
$sub->set('myKey1', 'value1')->expires('tomorrow');
$sub->set('myKey2', 'value2');
$sub->all(); //[]
$sub->commit(); //save values
$sub->all(); //['myKey1' => 'value1','myKey2' => 'value2']

$subOfSub = $sub::sub('subCollection');
$subOfSub->put('myKey1', 'value1');
$subOfSub->put('myKey2', 'value2');
$subOfSub->get('myKey2'); //outputs value2
$subOfSub->all(); //['myKey1' => 'value1','myKey2' => 'value2']
$sub::sub('subCollection')->get('myKey2'); //outputs value2
$subOfSub->all(); //['myKey1' => 'value1','myKey2' => 'value2']
```

Using a new instance for default adapter
----------------------------------------

[](#using-a-new-instance-for-default-adapter)

```
Cachly::instance('newInstance')->put('key1', 'key1 value');
Cachly::instance('newInstance')->all(); //outputs

/*
Array
(
    [key1] => key1 value
)
*/
```

Adapter instance shortcuts
--------------------------

[](#adapter-instance-shortcuts)

- yourOwnShortcut - see how to make own [shortcuts](#configure-your-own-adapter-with-shortcuts)

```
Cachly::sess('mySessionInstance')->put('key1', 'key1 value');
Cachly::sess('mySessionInstance')->all();
Cachly::$sess->put('key1', 'key1 value');
Cachly::$sess->all();

Cachly::mem('mySessionInstance')->put('key1', 'key1 value');
Cachly::mem('mySessionInstance')->all();
Cachly::$mem->put('key1', 'key1 value');
Cachly::$mem->all();
....
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance51

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~328 days

Recently: every ~274 days

Total

6

Last Release

49d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c13460ff76e7fb78d8597217aff4ea9bec2a5eca918bd02be8ecd8b9d6be4261?d=identicon)[infira](/maintainers/infira)

---

Top Contributors

[![infira](https://avatars.githubusercontent.com/u/3343899?v=4)](https://github.com/infira "infira (56 commits)")

### Embed Badge

![Health badge](/badges/infira-cachly/health.svg)

```
[![Health](https://phpackages.com/badges/infira-cachly/health.svg)](https://phpackages.com/packages/infira-cachly)
```

###  Alternatives

[incenteev/hashed-asset-bundle

Apply an asset version based on a hash of the asset for symfony/asset

25526.7k1](/packages/incenteev-hashed-asset-bundle)[rikudou/psr6-dynamo-db-bundle

PSR-6 and PSR-16 cache implementation using AWS DynamoDB for Symfony

2077.8k](/packages/rikudou-psr6-dynamo-db-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
