简体   繁体   English

Symfony2从数据库加载设置

[英]Symfony2 load settings from database

I have a db table (doctrine entity) that I use to store some editable settings for my app, like page title, maintenance mode (on/off), and some other things.. 我有一个db表(doctrine实体),我用它来存储我的应用程序的一些可编辑设置,如页面标题,维护模式(开/关),以及其他一些东西..

I can load the settings normally using the entity manager and repositories, but I think that's not the best solution... 我可以使用实体管理器和存储库正常加载设置,但我认为这不是最好的解决方案......

My questions are: - can I load the settings only once at some kernel event and then access them the same way I access any other setting saved in yml config files.. 我的问题是: - 我可以在某个内核事件中仅加载一次设置,然后以与访问yml配置文件中保存的任何其他设置相同的方式访问它们。

  • how can I cache the database settings, so I would only do one DB query, and then in future page requests, it would use the cached values, instead of doing a DB query for each page request? 我如何缓存数据库设置,所以我只会进行一次数据库查询,然后在将来的页面请求中,它会使用缓存的值,而不是对每个页面请求进行数据库查询? (of course, everytime I change something in the settings, I would need to purge that cache, so the new settings could take effect) (当然,每当我在设置中更改某些内容时,我都需要清除该缓存,因此新设置可能会生效)

LiipDoctrineCacheBundle provides a service wrapper around Doctrine's common Cache ( documentation ) that allows you to use several cache drivers like filesystem, apc, memcache, ... LiipDoctrineCacheBundle提供了一个围绕Doctrine的通用Cache( 文档 )的服务包装器,允许您使用多个缓存驱动程序,如filesystem,apc,memcache,...

I would recommend loading your generic container-parameters/settings (like maintainance mode,...) from database in a bundle-extension or a compiler-pass . 我建议在bundle-extensioncompiler-pass中从数据库加载通用容器参数/设置(如维护模式......)。

route-specific settings (like page title, ...) could be loaded in a kernel event listener . 特定于路由的设置(如页面标题,...)可以加载到内核事件侦听器中 You can find a list of kernel events here . 您可以在此处找到内核事件列表。

update/invalidate their cache using a doctrine postUpdate/postPersist/postRemove listener . 使用doctrine postUpdate / postPersist / postRemove 侦听器更新/使其缓存无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM