Sandip Pingle
Sandip Pingle

Reputation: 665

Yii caching share caching between console app and web app?

I am creating caching for my app using console app. But i unable share that cache for my web app. (in my redis database its showing created from console) any idea how can i share cache created from console to my web app?

Upvotes: 2

Views: 837

Answers (3)

leealex
leealex

Reputation: 1593

All you need is to set the same keyPrefix for both applications

'cache' => [
    'class'     => 'yii\caching\FileCache',
    'keyPrefix' => 'common'
],

Tested with Yii2 basic template.

Upvotes: 0

Mayur Bhamre
Mayur Bhamre

Reputation: 201

I was getting same issue which was resolved by keeping same setting for backend and frontend both apps.

'cache'=> array(
    'class' => 'CRedisCache',
    'hostname' => 'localhost',
    'port' => 6379,
    'database' => 0,
    'hashKey' => false,
    'keyPrefix' => '',
);

set keyPrefix to empty and hashKey to false,

if you use default setting for keyPrefix and hashKey CRedisCache will create different keys for same value provided from set command eg.

 Yii::app()->cache->set('foo', 'bar'); frontend server 
will create key in redis something like "0327f5f7378e9056c775ab69aa206322"

  Yii::app()->cache->set('foo', 'bar'); backend server 
ll create key in redis something like "d2c81df2db2285435c1975b5cb5c5b66"      

CRedisCache creates unique key by using combination of hashKey and keyPrefix for every requested server.

Upvotes: 3

Sandip Pingle
Sandip Pingle

Reputation: 665

Frontend configuration file:

'cache' => array(
    'class'     => 'system.caching.' . (!MW_DEBUG ? 'CFileCache' : 'CDummyCache'),
    'keyPrefix' => md5('frontend.' . MW_VERSION . Yii::getPathOfAlias('frontend')),
),

Console configuration file:

'cache' => array(
  'class'     => 'system.caching.' . (!MW_DEBUG ? 'CFileCache' : 'CDummyCache'),
  'keyPrefix' => md5('console.' . MW_VERSION . Yii::getPathOfAlias('backend')),
),

reference answer

Upvotes: 0

Related Questions