Redis simple to use

Simple operation

For example, display the configuration file for a given user.

   $user = Redis::get('user:profile:'.$id);

  Redis::set('user:profile:'.$id, "ok"); 

You can Redis call any of the Redis commands. Swoft uses the magic method to pass commands to the Redis server, so just pass the parameters required by the Redis command:

   Redis::set('name', 'Taylor');

  $values = Redis::lrange('names', 5, 10); 

All methods and operations are essentially the same as phpredis native, which is accessed through magic methods, making operations easier.

Of course, ordinary string settings can't satisfy our daily development, you can also use arrays.

   $setData = [
        'goods' => [
            'goods_id'   => 1,
            'goods_name' => 'iPhone xx'
         ]
  ];
  Redis::set($key, $setData); 

By calling the get method, underlying deserialized automatically ensure the deposit into, taken out of the data consistency.

zAdd to use the ordered collection zAdd

 $scores1 = [
    'key1' => 11,
    'key3' => 11,
    'key4' => 11,
    'key2' => 21,
];

// zAdd 11 key1 11 key2 11 key3 11 key4
$result1 = Redis::zAdd('key', $scores1); 

Key 成员 , value is the 分数 .

Members can not be repeated, the score can be.

Use Redis inject connection pool by @Inject

Injecting properties through Inject is the same as using 1.0

 <?php declare(strict_types=1);

namespace App\Http\Controller;

use Swoft\Redis\Pool;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;

/**
 *
 * @Controller("Redis")
 */
class RedisController
{

    /**
     * 例子 1: 如果 Inject 没有参数,会使用 var 定义的类型
     *
     * @Inject()
     *
     * @var Pool 默认连接使用的是 redis.pool
     */
    private $redis;

    /**
     * 例子 2: 如果 Inject 指定参数,会使用指定的 pool 注入到该属性. 和 var 定义的类型没关系
     *
     * @Inject("redis.inc.pool")
     *
     * @var Pool
     */
    private $redisInc;

    /**
     * @RequestMapping(route="find")
     *
     * @return array
     *
     * @throws Throwable
     */
    public function find(): array
    {
        $this->redis->set('user', ["name"=>"gimi", "age"=>"18"]);

        $this->redisInc->incr('user-count',1);

        return $this->redis->get('user');
    }
}   

You can in Inject specify that a redis connection pool, if Inject do not specify a connection pool, the default will use @var inject the specified type.

Specify a connection pool to get a connection

Of course all connections are allocated in the connection pool. If you want to use your own defined connection, you can use

 $poolName  = 'redis-clusters-pool'
$redis     =  Redis::connection($poolName);
$redis->get("a"); 

Redis::connection(); If no connection pool name is specified, the connection is obtained from the system-defined redis.pool connection pool by default. This method returns a 连接 instead of a 连接池 . Do not 共享连接 , 共享连接池

After the connection is obtained and the operation is consistent with the phpredis native usage extension, how to create a connection pool reference redis 设置 chapter, the default is obtained in the redis.pool connection pool

Cache hit ratio

If you want to see the hits obtained by a key , you can get it at your notice log level. The format is roughly a prefix like redis.hit/req.... , which helps you analyze the cache hit ratio.

Event listener

Before event

The underlying will throw a RedisEvent::BEFORE_COMMAND event before Redis executes

 
<?php declare(strict_types=1);

namespace App\Listener;

use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Redis\RedisEvent;

/**
 * Class FinishListener
 *
 * @since 2.0
 *
 * @Listener(event=RedisEvent::BEFORE_COMMAND)
 */
class RedisBeforeListener implements EventHandlerInterface
{
    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        [$method, $parameters] = $event->getParams();
    }
}
 

After event

After the Redis command, the RedisEvent::AFTER_COMMAND event will be thrown. You can listen for the execution of the command. If an exception occurs in the execute command, the RedisEvent::AFTER_COMMAND event will not be thrown.

 <?php declare(strict_types=1);

namespace App\Listener;

use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Redis\RedisEvent;

/**
 * Class FinishListener
 *
 * @since 2.0
 *
 * @Listener(event=RedisEvent::AFTER_COMMAND)
 */
class RedisAfterListener implements EventHandlerInterface
{
    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        [$method, $parameters, $result] = $event->getParams();
    }
}
 
/docs/2.x/en/redis/usage.html
progress-bar