【问题标题】:How to set or get value from cache in symfony2.8?如何在 symfony2.8 中从缓存中设置或获取值?
【发布时间】:2017-11-19 22:28:22
【问题描述】:

在我的 service.yml

cache:
    class: Doctrine\Common\Cache\PhpFileCache
    arguments: [%kernel.cache_dir%] 

在我的控制器

$cache = $this->get('cache');

if ($cache->fetch($cache_key) != NULL) {
    // mycode
}

我的 SilencedError

1-include(\app\cache\dev\30\446f637472696e654e616d65737061636543616368654b65795b5d.doctrinecache.php):无法打开流:没有这样的文件或目录

【问题讨论】:

  • 你有没有这样的文件夹在里面并且出错了?

标签: symfony caching


【解决方案1】:

您正在尝试获取一个条目而不检查它是否存在。在获取条目之前使用$cache->contains($cache_key)

由于PhpFileCache 类扩展FileCache 扩展CacheProvider,您应该检查这些类是否有可用的函数来创建和检索缓存条目:

/**
 * {@inheritdoc}
 */
public function fetch($id)
{
    ...
}

/**
 * {@inheritdoc}
 */
public function fetchMultiple(array $keys)
{
    ...
}

/**
 * {@inheritdoc}
 */
public function saveMultiple(array $keysAndValues, $lifetime = 0)
{
    ...
}

/**
 * {@inheritdoc}
 */
public function contains($id)
{
    ...
}

/**
 * {@inheritdoc}
 */
public function save($id, $data, $lifeTime = 0)
{
    ...
}

【讨论】:

  • 我将数据保存到缓存 $cache->save($cache_key,$data) 和我的类 FileCache ,CacheProvider 存在,但显示错误
猜你喜欢
  • 2018-08-12
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
相关资源
最近更新 更多