PHPackages                             jimersylee/yii-redis - 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. jimersylee/yii-redis

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

jimersylee/yii-redis
====================

Familiar Yii style access to redis

1.0.0(12y ago)06MITPHP

Since Nov 1Pushed 5y agoCompare

[ Source](https://github.com/jimersylee/yii-redis)[ Packagist](https://packagist.org/packages/jimersylee/yii-redis)[ RSS](/packages/jimersylee-yii-redis/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

YiiRedis
========

[](#yiiredis)

Provides object oriented access to Redis in a familiar Yii Style. When you add or remove items from redis entities (lists, sets, sorted sets, hashes), changes are pushed to the server immediately, this is useful when your application needs to make information available across multiple requests.

This package relies on the  PHP extension, please make sure this is installed before continuing.

Usage
-----

[](#usage)

### Configuring The Redis Connection

[](#configuring-the-redis-connection)

Add the following to your application config

```
[
"components" => array(
	"redis" => array(
		"class" => "packages.redis.ARedisConnection",
		"hostname" => "localhost",
		"port" => 6379,
		"database" => 1,
		"prefix" => "Yii.redis."
	),
	...
)];
```

### Storing and Retrieving simple keys

[](#storing-and-retrieving-simple-keys)

To store a simple value in a key and read it back:

```
Yii::app()->redis->getClient()->set("myKey", "Your Value");
echo Yii::app()->redis->getClient()->get("myKey"); // outputs "Your Value"
Yii::app()->redis->getClient()->del("myKey"); // deletes the key
```

### Using lists

[](#using-lists)

Redis lists are simple lists of values that are kept in the order that the items are added

```
$list = new ARedisList("aNameForYourListGoesHere");
$list->add("cats");
$list->add("dogs");
$list->add("fish");
foreach($list as $i => $val) {
	echo $val."";
}
$list->clear(); // delete the list
```

### Using sets

[](#using-sets)

Redis sets are unordered lists of non repeatable values

```
$set = new ARedisSet("aNameForYourSet");
$set->add(1);
$set->add(2);
$set->add(3);
echo $set->getCount(); // outputs 3
$set->add(3);
echo $set->getCount(); // still 3, cannot add the same value more than once
foreach($set as $val) {
	echo $val."";
}
```

### Using Sorted Sets

[](#using-sorted-sets)

Redis sorted sets are lists of non repeatable values where each value is associated with a score that affects the order of the results

```
$sortedSet = new ARedisSortedSet("aNameForYourSortedSet");
$sortedSet->add("myValue", 0.4);
$sortedSet->add("myOtherValue", 0.8);
$sortedSet->add("myOtherOtherValue", 0.9);
foreach($sortedSet as $key => $score) {
	echo $key.": ".$score."";
}
```

### Using Hashes

[](#using-hashes)

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth).

```
$hash = new ARedisHash("myHashNameHere");
$hash->whatever = "someValue";
$hash->greeting = "hello world";

echo $hash->getCount(); // outputs 2
```

### Using Pub/Sub

[](#using-pubsub)

Redis allows us to subscribe to channels and publish messages to them.

```
$channel = new ARedisChannel("myChan");
$channel->onReceiveMessage = function($redis, $channel, $message) {
	echo "Message Received: ".$message."\n";
};
$channel->publish("hello world"); // sends a messsage to the channel
$channel->subscribe(); // subscribes to the channel and listens to messages, blocks the process
```

### Using Redis for Counters

[](#using-redis-for-counters)

Often we need to store counters for a particular database table or row, with YiiRedis this is fast and easy.

```
$counter = new ARedisCounter("totalPageViews");
$counter->increment();
echo $counter->getValue();
```

### Using Redis for Mutexes

[](#using-redis-for-mutexes)

Mutexes are useful to ensure that only one client can access a particular resources at a time.

```
$mutex = new ARedisMutex("someOperation");
$mutex->block(); // blocks execution until the resource becomes available
// do something
$mutex->unlock(); // release the lock
```

### Using Redis for Caching

[](#using-redis-for-caching)

Redis is a good alternative to memcached for caching because its speed is comparable and the data is persisted.

In your app config:

```
[
"components" => array(
	"cache" => array(
		"class" => "packages.redis.ARedisCache"
	),
	...
),];
```

### Using Redis as a backend for Active Record

[](#using-redis-as-a-backend-for-active-record)

It is possible to store active record like structures in redis using ARedisRecord.

> Note: this is experimental functionality and may be subject to change

```
$record = ARedisRecord::model()->findByPk(1); // loads a record with a unique id of 1
$record->name = "a test name"; // sets the name attribute on the record
$record->somethingElse = "some other value";
$record->save(); // saves the record to redis
$record->delete(); // deletes the record from redis
```

todo 测试channel

修复ARedisEntity-&gt;expire()的语法错误 删除过时的方法 如redis-&gt;delete,redis-lsize

修复ARedisList-&gt;copyFrom()方法中的语法错误 修复ARedisSet-&gt;copyFrom()方法中的语法错误

解决ARedisHash中类销毁时会自动设置过期时间为10天的bug

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Unknown

Total

1

Last Release

4627d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/29f2a59d2b8625c68408655425ba7ae5e1700ee24ee13d07403c94a9449b1b4d?d=identicon)[jimersylee](/maintainers/jimersylee)

---

Top Contributors

[![phpnode](https://avatars.githubusercontent.com/u/363611?v=4)](https://github.com/phpnode "phpnode (23 commits)")[![vgoodvin](https://avatars.githubusercontent.com/u/373720?v=4)](https://github.com/vgoodvin "vgoodvin (10 commits)")[![andremetzen](https://avatars.githubusercontent.com/u/636229?v=4)](https://github.com/andremetzen "andremetzen (4 commits)")[![rolies106](https://avatars.githubusercontent.com/u/1167249?v=4)](https://github.com/rolies106 "rolies106 (4 commits)")[![rishav](https://avatars.githubusercontent.com/u/45494?v=4)](https://github.com/rishav "rishav (3 commits)")[![samsonradu](https://avatars.githubusercontent.com/u/2971447?v=4)](https://github.com/samsonradu "samsonradu (2 commits)")[![reklatsmasters](https://avatars.githubusercontent.com/u/2255752?v=4)](https://github.com/reklatsmasters "reklatsmasters (1 commits)")[![markuspaks](https://avatars.githubusercontent.com/u/30629?v=4)](https://github.com/markuspaks "markuspaks (1 commits)")[![maxyc](https://avatars.githubusercontent.com/u/795360?v=4)](https://github.com/maxyc "maxyc (1 commits)")

### Embed Badge

![Health badge](/badges/jimersylee-yii-redis/health.svg)

```
[![Health](https://phpackages.com/badges/jimersylee-yii-redis/health.svg)](https://phpackages.com/packages/jimersylee-yii-redis)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198741.5k12](/packages/pgvector-pgvector)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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