LoginSignup
0
0

More than 3 years have passed since last update.

Redis 安易になんでもSETしてたらGETできなくてハマった

Posted at

サンプルコード

<?php

namespace App\Controller\Component;

use Cake\Controller\Component;
use Cake\Core\Configure;
use Predis\Client;

class HogeCacheManagerComponent extends Component
{
    /** @var object */
    private $client;

    /**
     * initialize
     *
     * @param  array $registory
     * @return void
     */
    public function initialize(array $registory)
    {
        parent::initialize($registory);

        $this->client = new Client(Configure::read('Redis'));
    }

    /**
     * getCache method
     *
     * @param  string      $key
     * @return null|string
     */
    public function getCache($key)
    {
        return $this->client->get($key);
    }

    /**
     * setCache method
     *
     * @param  string $key
     * @param  mixed  $value
     * @return void
     */
    public function setCache($key, $value, $expire = 600)
    {
        $this->client->set($key, json_encode($value));
        $this->client->expire($key, $expire);
    }
}

テストコード

<?php
namespace App\Test\TestCase\Controller\Component;

use App\Controller\Component\HogeCacheManagerComponent;
use Cake\Controller\ComponentRegistry;
use Cake\TestSuite\TestCase;

/**
 * App\Controller\Component\HogeCacheManagerComponent Test Case
 */
class HogeCacheManagerComponentTest extends TestCase
{

    /**
     * Test subject
     *
     * @var \App\Controller\Component\HogeCacheManagerComponent
     */
    private $HogeCacheManager;

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $registry = new ComponentRegistry();
        $this->HogeCacheManager = new HogeCacheManagerComponent($registry);
    }

    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown()
    {
        unset($this->HogeCacheManager);

        parent::tearDown();
    }

    /**
     * Test getCache method
     *
     * @return void
     */
    public function testGetCache()
    {
        $this->assertEmpty($this->HogeCacheManager->getCache('fruits'));

        $this->HogeCacheManager->setCache('fruits', 'apple');
        $this->assertSame('apple', str_replace('"', '', $this->HogeCacheManager->getCache('fruits')));
    }

    /**
     * Test setCache method
     *
     * @return void
     */
    public function testSetCache()
    {
        $this->assertEmpty($this->HogeCacheManager->getCache('animals'));

        $this->HogeCacheManager->setCache('animals', 'dog');
        $this->assertSame('dog', str_replace('"', '', $this->HogeCacheManager->getCache('animals')));
    }
}

SETの仕様

SET(key, value)
計算時間: O(1)
文字列値 value をキー key にセットします。文字列は1073741824バイト(1GB)以下でなければいけません。

返り値
Status code replyを返す。

引用元:http://redis.shibu.jp/commandreference/strings.html#command-SET

GETの仕様

GET(key)
計算時間: O(1)
指定したキー key に対応する値を取得します。もしキーが存在しなかったら特別な値 “nil” を返します。もしキーに対応する値が文字列型ではなかったらエラーが返ります。なぜなら GET は文字列型にしか対応していないからです。

返り値
Bulk replyを返す。

引用元:http://redis.shibu.jp/commandreference/strings.html#command-GET

ハマったこと

  • SET時に keyを文字列以外の値にする

なぜハマったのか

  • 上記に記されてるように、SETの仕様はバイト制限のみであり、型制限は設けられていない
  • そのため、型を意識せずとも セットしたいkeyvalueでセットできてしまう

ポイント

  • SET時のkey文字列型 にする
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0