使用swoft框架的redis一定得先安装hiRedis
1.docker run --name myredis -d -p 6379:6379 redis:alpine (docker安装)
2.docker exec -it myredis redis-cli (测试)
<?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');
}
}
启动服务,查看,测试成功。