YiiRedis 131

A set of wrappers for different data types in redis

phpnode/YiiRedis

Written in PHP by 9 contributors

Contribute

Need Support?


we offer maintenance, support and development services for all our open source projects.

Contact Us

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 https://github.com/nicolasff/phpredis PHP extension, please make sure this is installed before continuing.

Usage

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

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

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."<br />";
}
$list->clear(); // delete the list

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->count; // outputs 3
$set->add(3);
echo $set->count; // still 3, cannot add the same value more than once
foreach($set as $val) {
    echo $val."<br />";
}

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."<br />";
}

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->count; // outputs 2

Using Pub/Sub

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

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

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

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

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




43 other PHP projects


phpwkhtmltopdfPHP 716

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

php-pdftkPHP 213

A PDF conversion and form utility based on pdftk

yii2-localeurlsPHP 189

Automatic locale/language management for URLs

yiipasswordPHP 81

Password strategies for Yii

yii2-dockerizedPHP 64

A template for docker based Yii 2 applications

YiiSolrPHP 50

A wrapper for the pecl solr library that provides common Yii constructs, such as models, data providers etc

php-shellcommandPHP 48

A simple object oriented interface to execute shell commands in PHP

Yii-Docs-GeneratorPHP 47

Generates HTML documentation for Yii applications

YiiGitPHP 36

A git wrapper for Yii, allows access to all git commands programatically

YiiElasticSearchPHP 32

Elastic Search client for Yii

YiiCurlPHP 30

A curl library for Yii

YiiStateMachinePHP 28

A state machine behavior for Yii

yii2-streamlogPHP 19

A Yii 2 log target for streams in URL format

oauth2yiiPHP 18

An OAuth2 client / server extension for the Yii framework

packagecompressorPHP 16

A Javascript/CSS compressor based on Yii's package system

restyiiPHP 16

A RESTful extension for Yii.

localeurlsPHP 12

Automatic locale/language management for URLs

php-excel-readerPHP 12

It reads the binary format of XLS files directly and can return values and formats from any cell. This project is the fork of http://code.google.com/p/php-excel-reader/ that, apparently, is no longer maintained.

pdfablePHP 10

A Yii extension to create PDFs with PHPWkHtmlToPdf/wkhtmltopdf

yii-api-vimPHP 8

Yii API manual plugin for VIM

yii2-excel-messagePHP 7

Translate messages via Excel files

yii2-bs3activeformPHP 7

A Bootstrap 3 enhanced ActiveForm for Yii 2

xcrudcontrollerPHP 7

A base class to quickly build customized CRUD interfaces.

handlebarsphpPHP 6

Transpiles handlebars templates into native PHP templates

Yii-Package-ManagerPHP 6

A package manager for Yii

php-tmpfilePHP 6

A convenience class for temporary files

yii2-apidoc-vimPHP 5

Yii2 apidoc as Vim helpfiles

php-orientdbPHP 5

A fast PHP driver for the OrientDB binary protocol.

translatablePHP 4

Transparent attribute translation for ActiveRecords

YiiUsersPHP 4

User management for Yii

Yii-Resource-ManagerPHP 3

Helpers to allow Yii to interact with resources (files) of different types

flushablePHP 3

Yii dependency that allows to flush records from the cache

bs3activeformPHP 2

A lightweight utility to render Bootstrap 3 forms in Yii

YiiEmailerPHP 2

Emailing functions for Yii

AccessRestrictablePHP 2

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.

YiiLinkablePHP 1

A simple extension for Yii allowing easy and consistent access to model URLs and appropriate anchor text.

defaultpersisterPHP 1

Yii extension to save and restore model values in user session.

hybridauthmanagerPHP 1

An AuthManager for Yii that stores the hierarchy in a flat PHP file and the assignments in DB

YiiYaaPHP 1

YAA is an additional abstraction layer for Yii that aggregates a number of child models into a clean single model that is easy to cache

YiiAbstractArrayModelPHP

Work with PHP files in Active Record way

twittonioPHP

simple useless twitter client

yii2-base-appPHP

An alternative Yii2 application template for purists

xreturnablePHP

Yii extension to create URLs that allow to return to a page by storing its GET Parameters on a stack.