Symfony和Predis:无法获取密钥

时间:2019-06-29 17:06:36

标签: symfony redis docker-compose predis

我尝试获得一个运行symfony / predis的简单示例。但是,如果我尝试在docker redis服务器上存储/读取某些内容,则会收到以下错误消息:

从服务器读取行时出错。 [tcp:// localhost:5378]

  key" => "thisIsACacheKey"
  "exception" => ConnectionException^ {#3532 ▼
    -connection: StreamConnection {#3766 …}
    #message: "Error while reading line from the server. [tcp://localhost:5378]"
    #code: 0
    #file: "C:\cacheExmaple\vendor\predis\predis\src\Connection\AbstractConnection.php"
    #line: 155

我不知道docker或symfony / predis配置是否存在问题。 cache.yaml:

framework:
    cache:
        # Put the unique name of your app here: the prefix seed
        # is used to compute stable namespaces for cache keys.
        prefix_seed: aj/cacheExample

        # Redis
        app: cache.adapter.redis
        default_redis_provider: redis://localhost:5378

docker ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
8c18a3e37e74        redis:3.2-alpine    "docker-entrypoint.s…"   43 seconds ago      Up 41 seconds       0.0.0.0:5378->5378/tcp, 6379/tcp    projectmonatg_redis_1

这就是示例HomeController来测试缓存功能。

class HomeController extends AbstractController
{
    public function __construct(AdapterInterface $cache)
    {
        $this->cache = $cache;
    }

    /**
     * @Route("/", name="home")
     */
    public function index()
    {
        $cacheKey = 'thisIsACacheKey';
        $item = $this->cache->getItem($cacheKey);

        $itemCameFromCache = true;
        if (!$item->isHit()) {
            $itemCameFromCache = false;
            $item->set('this is some data to cache');
            $item->expiresAfter(new DateInterval('PT10S')); // the item will be cached for 10 seconds
            $this->cache->save($item);

        }

        return $this->render('home/index.html.twig', ['isCached' => $itemCameFromCache ? 'true' : 'false']);
    }
}

0 个答案:

没有答案