filth/redis-bundle

Provides OO-Redis handling

Installs: 922

Dependents: 0

Suggesters: 0

Security: 0

Stars: 8

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

dev-master 2013-07-10 00:53 UTC

This package is not auto-updated.

Last update: 2024-04-13 10:59:56 UTC


README

The FilthRedisBundle adds support for object oriented approach in Symfony2 to the Redis key - value storage. This bundle helps you to keep track of the used Redis keys. It will ensure that no keys will overlap unintentionally

Installation

Step 1: Download the FilthRedisBundle

Ultimately, the FilthRedisBundle files should be downloaded to the 'vendor/bundles/Filth/RedisBundle' directory.

You can accomplish this several ways, depending on your personal preference. The first method is the standard Symfony2 method.

Using the vendors script

Add the following lines to your deps file:

    [FilthRedisBundle]
        git=https://github.com/filthz/RedisBundle.git
        target=/bundles/Filth/RedisBundle

Now, run the vendors script to download the bundle:

$ php bin/vendors install

Using submodules

If you prefer instead to use git submodules, then run the following:

$ git submodule add git://github.com/filthz/RedisBundle.git vendor/bundles/Filth/RedisBundle
$ git submodule update --init

Step 2: Configure the Autoloader

Now you will need to add the Filth namespace to your autoloader:

<?php
// app/autoload.php

$loader->registerNamspaces(array(
    // ...
    'Filth' => __DIR__.'/../vendor/bundles',
));

Step 3: Enable the bundle

Finally, enable the bundle in the kernel:

<?php
// app/appKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Filth\RedisBundle\FilthRedisBundle(),
    );
}

Step 4: Install Predis

The FilthRedisBundle uses Predis, see https://github.com/nrk/predis for installation details

Use Case and Usage

Imagine you have a lot of pictures on your website, each time a picture is displayed a view counter should be incremented. Usually you dont want to terrorize your DB and issue a query on each view. Instead you collect these views and insert a bunch of them in a single query. This is where FilthRedisBundle comes into play.

Step 5: Create an Redis Entity

<?php

# src/Foo/Redis/EventPictureViewsRedisEntity.php

namespace Foo\Redis;

use Filth\RedisBundle\Annotation\RedisAnnotation;
use Filth\RedisBundle\Entity\BaseRedisEntity;

/**
 * @RedisAnnotation(redis_key="eventpic_views_{EVENTID}_{PICTUREID}")
 */
class EventPictureViewsRedisEntity extends BaseRedisEntity
{
    /**
     * @RedisAnnotation(required=true)
     */
    protected $eventID        = null;

    /**
     * @RedisAnnotation(required=true)
     */
    protected $eventPictureID = null;


    public function getEventID()
    {
        return $this->eventID;
    }

    public function getEventPictureID()
    {
        return $this->eventPictureID;
    }

    public function setEventID($eventID)
    {
        $this->eventID = $eventID;
    }

    public function setEventPictureID($eventPictureId)
    {
        $this->eventPictureID = $eventPictureId;
    }
}

Notice that we defined our redis key with an annotation ( @RedisAnnotation(redis_key="eventpic_views_{EVENTID}_{PICTUREID}") ) 'FilthRedisBundle' will take care of the choosen keys and will not let you define the same key among different entities.

In my example there are 2 values required for later processing: $eventID and $eventPictureID. Both have been marked as required. You need to create setter and getter for these fields.

Step 6: Register the new entity

# app/config/config.yml

filth_redis:
   entities:
       - { alias: EVENTPICTURE_VIEWS, class: Foo\Redis\EventPictureViewsRedisEntity }

We are ready to roll!

Example

Get the entity and set the required values:

    $redisClient  = $this->get('snc_redis.default_client');
    $redisFactory = $this->get('filth.redis.factory');
    $redisRepo    = new RedisRepository($redisClient);
    
    $picViewEntity = $redisFactory->getRedisEntityByAlias('EVENTPICTURE_VIEWS');
    $picViewEntity->setEventID(123);
    $picViewEntity->setEventPictureID(567);
    
    $redisRep->increase($picViewEntity);

Retrieve all Keys (f.e. different eventID or pictureID) for an Redis Entity:

    $redisClient  = $this->get('snc_redis.default_client');
    $redisFactory = $this->get('filth.redis.factory');
    $redisRepo    = new RedisRepository($redisClient);
    
    $picViewEntity = $redisFactory->getRedisEntityByAlias('EVENTPICTURE_VIEWS');
    $keys = $redisRepo->getKeys($picViewEntity);

Retrieve an Redis Entity by key

    $redisClient  = $this->get('snc_redis.default_client');
    $redisFactory = $this->get('filth.redis.factory');
    $redisRepo    = new RedisRepository($redisClient);
    
    $keys = $redisRepo->getKeys($picViewEntity);
    
    foreach($keys as $key)
    {
        $redisEntity = $redisRepo->getRedisEntityByKey($key, $redisFactory);
    }

$redisEntity will be an Foo\Redis\EventPictureViewsRedisEntity Object and its values ($eventID, $eventPictureID and $value) will be set automaticly by 'FilthRedisBundle'. You can access these values with the getter functions of the Entity:

    $redisClient  = $this->get('snc_redis.default_client');
    $redisFactory = $this->get('filth.redis.factory');
    $redisRepo    = new RedisRepository($redisClient);
    
    $keys = $redisRepo->getKeys($picViewEntity);
    
    foreach($keys as $key)
    {
        $redisEntity = $redisRepo->getRedisEntityByKey($key, $redisFactory);
        
        $eventID   = $redisEntity->getEventID();
        $pictureID = $redisEntity->getEventPictureID();
        $value     = $redisEntity->getValue();
    }