filth/redis-bundle

提供OO-Redis处理

安装: 922

依赖项: 0

建议者: 0

安全: 0

星星: 8

关注者: 3

分支: 1

开放问题: 0

类型:symfony-bundle

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

This package is not auto-updated.

Last update: 2024-09-28 12:58:40 UTC


README

FilthRedisBundle 为 Symfony2 添加了对 Redis 键值存储面向对象方法的支持。此包可以帮助您跟踪使用的 Redis 键。它将确保不会无意中发生键重叠。

安装

步骤 1:下载 FilthRedisBundle

最终,FilthRedisBundle 文件应下载到 'vendor/bundles/Filth/RedisBundle' 目录。

您可以通过多种方式完成此操作,具体取决于您的个人喜好。第一种方法是标准的 Symfony2 方法。

使用 vendors 脚本

将以下行添加到您的 deps 文件中

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

现在,运行 vendors 脚本来下载包

$ php bin/vendors install

使用子模块

如果您更愿意使用 git 子模块,请运行以下命令

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

步骤 2:配置自动加载器

现在您需要将 Filth 命名空间添加到您的自动加载器

<?php
// app/autoload.php

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

步骤 3:启用包

最后,在内核中启用包

<?php
// app/appKernel.php

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

步骤 4:安装 Predis

FilthRedisBundle 使用 Predis,有关安装详情请参阅 https://github.com/nrk/predis

用例和用法

想象一下,您网站上有很多图片,每次显示图片时都应该增加一个查看计数器。通常您不希望惊扰数据库并为每次查看发出查询。相反,您应该收集这些查看并一次性插入多个查询。这就是 FilthRedisBundle 发挥作用的地方。

步骤 5:创建 Redis 实体

<?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;
    }
}

请注意,我们使用注释(@RedisAnnotation(redis_key="eventpic_views_{EVENTID}_{PICTUREID}")) 定义了我们的 redis 键。'FilthRedisBundle' 将处理选择的键,并确保您不会在多个实体中定义相同的键。

在我的示例中,需要两个值用于后续处理:$eventID 和 $eventPictureID。这两个值都已标记为必需。您需要为这些字段创建设置器和获取器。

步骤 6:注册新实体

# app/config/config.yml

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

我们现在准备就绪!

示例

获取实体并设置所需值

    $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);

检索所有键(例如不同的事件ID或图片ID)的 Redis 实体

    $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);

通过键检索 Redis 实体

    $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 将是一个 Foo\Redis\EventPictureViewsRedisEntity 对象,其值($eventID、$eventPictureID 和 $value)将由 'FilthRedisBundle' 自动设置。您可以使用实体的获取器函数访问这些值。

    $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();
    }