Skip to content

Caching a query to the db with BLOB data type #9899

@lav45

Description

@lav45

Create table for PostgreSQL

create table "settings"
(
    "id"  varchar(128) not null,
    "data"   BLOB,
    primary key ("id")
);

INSERT INTO "public"."settings" VALUES ('6a3ce1a0bffe8eeb6fa986caf443e24c', 'a:1:{s:13:"template";s:1:"1";}');

Cache Query

$result = Yii::$app->db->cache(function($db) use ($key){
    return (new Query())
        ->select(['data'])
        ->from('settings')
        ->where(['id' => $key])
        ->createCommand($db)
        ->queryScalar();
});
// First run return
var_export($result); // 'a:1:{s:13:"template";s:1:"1";}'

// After the request has been cached return
var_export($result); // 0

This version of the caching works fine

protected function getValue($key)
{
    $cache = Yii::$app->cache;

    $result = $cache->get($key);

    if ($result === false) {
        $result = (new Query())
            ->select(['data'])
            ->from('settings')
            ->where(['id' => $key])
            ->createCommand()
            ->queryScalar();

        $cache->set($key, $result);
    }

    return $result;
}

Perhaps this bug will be in other database with BLOB data type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions