PHPackages                             colinmollenhour/credis - 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. [Database &amp; ORM](/categories/database)
4. /
5. colinmollenhour/credis

ActiveLibrary[Database &amp; ORM](/categories/database)

colinmollenhour/credis
======================

Credis is a lightweight interface to the Redis key-value store which wraps the phpredis library when available for better performance.

v1.17.1(2mo ago)36440.6M—1.1%144[1 issues](https://github.com/colinmollenhour/credis/issues)[1 PRs](https://github.com/colinmollenhour/credis/pulls)20MITPHPPHP &gt;=7.4.0CI passing

Since Nov 19Pushed 2mo ago19 watchersCompare

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

READMEChangelog (10)DependenciesVersions (34)Used By (20)

[![Build Status](https://github.com/colinmollenhour/credis/actions/workflows/ci.yml/badge.svg)](https://github.com/colinmollenhour/credis/actions/workflows/ci.yml/badge.svg)

Credis
======

[](#credis)

Credis is a lightweight interface to the [Redis](http://redis.io/) key-value store which wraps the [phpredis](https://github.com/nicolasff/phpredis)library when available for better performance. This project was forked from one of the many redisent forks.

Getting Started
---------------

[](#getting-started)

Credis\_Client uses methods named the same as Redis commands, and translates return values to the appropriate PHP equivalents.

```
require 'Credis/Client.php';
$redis = new Credis_Client('localhost');
$redis->set('awesome', 'absolutely');
echo sprintf('Is Credis awesome? %s.\n', $redis->get('awesome'));

// When arrays are given as arguments they are flattened automatically
$redis->rpush('particles', array('proton','electron','neutron'));
$particles = $redis->lrange('particles', 0, -1);
```

Redis error responses will be wrapped in a CredisException class and thrown.

Credis\_Client also supports transparent command renaming. Write code using the original command names and the client will send the aliased commands to the server transparently. Specify the renamed commands using a prefix for md5, a callable function, individual aliases, or an array map of aliases. See "Redis Security": for more info.

Contributing
------------

[](#contributing)

Please be sure to add tests to cover and new or changed functionality and run the PHP-CS-Fixer to format the code.

```
composer require "friendsofphp/php-cs-fixer:^3.13" --dev --no-update -n
composer format
```

Supported connection string formats
-----------------------------------

[](#supported-connection-string-formats)

```
$redis = new Credis_Client(/* connection string */);
```

### Unix socket connection string

[](#unix-socket-connection-string)

`unix:///path/to/redis.sock`

### TCP connection string

[](#tcp-connection-string)

`tcp://host[:port][/persistence_identifier]`

### TLS connection string

[](#tls-connection-string)

`tls://host[:port][/persistence_identifier]`

or

`tlsv1.2://host[:port][/persistence_identifier]`

Before php 7.2, `tls://` only supports TLSv1.0, either `ssl://` or `tlsv1.2` can be used to force TLSv1.2 support.

Recent versions of redis do not support the protocols/cyphers that older versions of php default to, which may result in cryptic connection failures.

#### Enable transport level security (TLS)

[](#enable-transport-level-security-tls)

Use TLS connection string `tls://127.0.0.1:6379` instead of TCP connection `tcp://127.0.0.1:6379` string in order to enable transport level security.

```
require 'Credis/Client.php';
$redis = new Credis_Client('tls://127.0.0.1:6379');
$redis->set('awesome', 'absolutely');
echo sprintf('Is Credis awesome? %s.\n', $redis->get('awesome'));

// When arrays are given as arguments they are flattened automatically
$redis->rpush('particles', array('proton','electron','neutron'));
$particles = $redis->lrange('particles', 0, -1);
```

Clustering your servers
-----------------------

[](#clustering-your-servers)

Credis also includes a way for developers to fully utilize the [scalability of Redis cluster](https://redis.io/docs/latest/operate/oss_and_stack/management/scaling/) by using Credis\_Cluster which is an adapter for the RedisCluster class from [the Redis extension for PHP](https://github.com/phpredis/phpredis). This also works on [AWS ElastiCatch clusters](https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Clusters.html). This feature requires the PHP extension for its functionality. Here is an example how to set up a cluster:

### Basic clustering example

[](#basic-clustering-example)

```
