php 如何在 Symfony 2 中缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8893081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:43:45  来源:igfitidea点击:

How to cache in Symfony 2?

phpcachingsymfony

提问by Tower

I need to cache some application specific data using Symfony 2's caching system so that I can run cache:clearto clear it. All the cache relies under app/cachebut how do I actually go about caching data?

我需要使用 Symfony 2 的缓存系统缓存一些特定于应用程序的数据,以便我可以运行cache:clear以清除它。所有缓存都依赖于,app/cache但我实际上如何缓存数据?

http://symfony.com/doc/current/cookbook/index.html

http://symfony.com/doc/current/cookbook/index.html

The only topic I see is about HTML caching with Varnish.

我看到的唯一主题是关于使用 Varnish 缓存 HTML。

回答by Kris Wallsmith

If you are using Doctrine already just use those cache classes.

如果您已经在使用 Doctrine,请使用这些缓存类。

Add a service to config.yml:

添加服务到config.yml

services:
    cache:
        class: Doctrine\Common\Cache\ApcCache

And use it in your controller:

并在您的控制器中使用它:

if ($fooString = $this->get('cache')->fetch('foo')) {
    $foo = unserialize($fooString);
} else {
    // do the work
    $this->get('cache')->save('foo', serialize($foo));
}

回答by Максим Шатов

Simple way use Doctrine cache providers. At first, register service(sample in config.yml):

使用Doctrine 缓存提供程序的简单方法。首先,注册服务(config.yml 中的示例):

services:
    memcached:
        class: Memcached
        calls:
            - [ addServer, ['localhost', 11211] ]
    memcached_cache:
        class: Doctrine\Common\Cache\MemcachedCache
        calls:
            - [ setMemcached, [@memcached] ]

Then to use get service, for example in controler:

然后使用 get 服务,例如在控制器中:

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

to send in another service use calls:

发送另一个服务使用调用

calls:
    - [ setCacheProvider, [@memcached_cache] ]

or arguments:

或参数:

arguments:
    - @memcached_cache

In the same way, you can use other interfaces of Doctrine Cache package. Doctrine Cache provides a very simple interface for which several out of the box implementations are provided:

同理,您可以使用 Doctrine Cache 包的其他接口。Doctrine Cache 提供了一个非常简单的接口,其中提供了几个开箱即用的实现:

  • ApcCache (requires ext/apc)
  • ArrayCache (in memory, lifetime of the request)
  • FilesystemCache (not optimal for high concurrency)
  • MemcacheCache (requires ext/memcache)
  • MemcachedCache (requires ext/memcached)
  • PhpFileCache (not optimal for high concurrency)
  • RedisCache.php (requires ext/phpredis)
  • WinCacheCache.php (requires ext/wincache)
  • XcacheCache.php (requires ext/xcache)
  • ZendDataCache.php (requires Zend Server Platform)
  • ApcCache(需要 ext/apc)
  • ArrayCache(在内存中,请求的生命周期)
  • FilesystemCache(不是高并发的最佳选择)
  • MemcacheCache(需要 ext/memcache)
  • MemcachedCache(需要 ext/memcached)
  • PhpFileCache(不是高并发的最佳选择)
  • RedisCache.php(需要ext/phpredis)
  • WinCacheCache.php(需要 ext/wincache)
  • XcacheCache.php(需要ext/xcache)
  • ZendDataCache.php(需要 Zend 服务器平台)

If you do not already use Doctrine, you may require Common Library for Doctrine projects: php composer.phar require doctrine/commonor require only Caching library offering an object-oriented API for many cache backends: php composer.phar require doctrine/cache

如果您还没有使用 Doctrine,您可能需要Doctrine 项目的公共库php composer.phar require doctrine/common或者只需要为许多缓存后端提供面向对象的 API 的缓存库php composer.phar require doctrine/cache

How to use Doctrine Caching you can read in Doctrine Common documentationon Doctrine Project web site

如何使用 Doctrine Caching,您可以在Doctrine Project 网站上的Doctrine Common 文档中阅读

回答by Alsatian

Symfony 3.1 provide a new Cache component.

Symfony 3.1 提供了一个新的缓存组件

回答by Benjamin

Symfony2 does not provide any component for application layer caching.

Symfony2 没有为应用层缓存提供任何组件。

Like you were already told, you can use the Doctrine Common caching library http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html

就像您已经被告知的那样,您可以使用 Doctrine Common 缓存库http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html

If you want something more advanced, you can also use one of the cache bundle provided by the community. For instance, the https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundlewhich provides tools for a good caching strategy.

如果你想要更高级的东西,你也可以使用社区提供的缓存包之一。例如,https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundle为良好的缓存策略提供了工具。

回答by Damien

There is no partial cache in Symfony2, the build-in cache is full HTTP only. You have to use a reverse proxy, and if you only want to cache a piece of code, you have to use ESI. It's maybe more work than with symfony 1 but performances worth it.

Symfony2 中没有部分缓存,内置缓存只有完整的 HTTP。你必须使用反向代理,如果你只想缓存一段代码,你必须使用 ESI。这可能比使用 symfony 1 工作更多,但性能值得。

Anyway, nothing stop you to use a memcached and store some stuff in it, look at this Bundlei.e. If as your question state it, you only have data to store, that's perfect (and a memcache cache is much faster than a filesystem one).

无论如何,没有什么能阻止你使用 memcached 并在其中存储一些东西,看看这个 Bundle,即如果你的问题说明了它,你只有数据要存储,那就完美了(并且 memcache 缓存比文件系统快得多) .