Create a new file /config/factories.yml (common for all applications)
or edit application-level /apps/%application_name%/config/factories.yml file.
Often, if you have back-end for managing data and front-end to print them out
you should enable cache in both of them. That is because you edit/create/delete
records in back-end, so object tags should be updated to invalidate front-end cache.
I recommend you to create default factories.yml for all applications you have by
creating a file /config/factories.yml (you could find explained
and short working examples bellow).
Symfony will check for this file and will load it as a default factories.yml
configuration for all applications you have in the project.
This is /config/factories.yml content (you can copy&paste this code
into your brand new created file) or merge this config with each application
factories.yml file.
Explained and commented working example of file /config/factories.yml
all:
view_cache:
class: sfTaggingCache
param:
cache:
class: sfMemcacheTaggingCache # Content will be stored in Memcache
# Here you can switch to any other backend
# (see Restrictions block for more info)
param:
persistent: true
storeCacheInfo: true
host: localhost
port: 11211
lifetime: 86400 # default value is 1 day (in seconds)
tags: ~ # storage for tags (could be the same as
# the cache storage)
# if "tags" is NULL (~), it will
# be the same as cache (e.i. sfMemcacheTaggingCache)
logger:
class: sfFileCacheTagLogger # to disable logger, set class to "sfNoCacheTagLogger"
param:
file: %SF_LOG_DIR%/cache_%SF_ENVIRONMENT%.log
file_mode: 0640 # -rw-r----- (default: 0640)
dir_mode: 0750 # drwxr-x--- (default: 0750)
time_format: "%Y-%b-%d %T%z" # e.g. 2010-Sep-01 15:20:58+0300 (default: "%Y-%b-%d %T%z")
format: %char% # %char% - Operation char (see char explanation in sfCacheTagLogger::explainChar())
# %char_explanation% - Operation explanation string
# %time% - Time, when data/tag was accessed
# %key% - Cache name or tag name with its version
# %microtime% - Microtime timestamp when data/tag was accessed
# %EOL% - Whether to append \n in the end of line
# (e.g. "%microtime% %char% (%char_explanation%) %key%%EOL%")
view_cache_manager:
class: sfViewCacheTagManager # Extended sfViewCacheManager class
#param:
# … your params here
Short working example to start caching with tags using APC (location: /config/factories.yml)
dev:
view_cache:
param:
logger:
param:
# extended log format for dev environment
format: "%char% %microtime% %key%%EOL%"
all:
view_cache:
class: sfTaggingCache
param:
cache:
class: sfAPCTaggingCache
param: []
tags: ~
logger:
class: sfCacheTagLogger
param:
file: %SF_LOG_DIR%/cache_%SF_ENVIRONMENT%.log
format: %char%
view_cache_manager:
class: sfViewCacheTagManager
param:
cache_key_use_vary_headers: true
cache_key_use_host_name: true
Restrictions: Backend's class should be inherited from sfCache
class. Then, it should be implement sfTaggingCacheInterface
(due a Doctrine cache engine compatibility).
Also, it should support the caching of objects and/or arrays.
Therefor, plugin comes with additional extended cache backend classes:
- sfAPCTaggingCache
- sfEAcceleratorTaggingCache
- sfFileTaggingCache
- sfMemcacheTaggingCache
- sfSQLiteTaggingCache
- sfXCacheTaggingCache
And one bonus cache backend:
- sfSQLitePDOTaggingCache (based on stand alone sfSQLitePDOCache)
Add "Cachetaggable" behavior to each model, which you want to cache
Example of file /config/doctrine/schema.yml
YourModel:
tableName: your_model
actAs:
## CONFIGURATION SHORT VERSION
## Cachetaggable will detect your primary keys automatically
## and generates uniqueKeyFormat based on PK column count
## (e.g. '%s_%s' if table contains 2 primary keys)
Cachetaggable: ~
## CONFIGURATION EXPLAINED VERSION
#Cachetaggable:
# uniqueColumn: id # you can customize unique column name (default is all table primary keys)
# versionColumn: object_version # you can customize version column name (default is "object_version")
# uniqueKeyFormat: '%s' # you can customize key format (default is "%s")
#
# # if you have more then 1 unique column, you could pass all of them
# # as array (tag name will be based on all of them)
#
# uniqueColumn: [id, is_enabled]
# uniqueKeyFormat: '%d-%02b' # the order of unique columns
# # matches the "uniqueKeyFormat" template variables order
Enable cache in settings.yml and add additional helpers to
standard_helpers section
To setup cache, often, is used a separate environment named "cache",
but in the same way you can do it in any other environments which you already have.
prod:
.settings:
cache: true
cache:
.settings:
cache: true
all:
.settings:
cache: false
Add helpers to the each application:
all:
.settings:
standard_helpers:
# … other helpers
- Partial # build-in Symfony helper to work with partials/components
- Cache # build-in Symfony helper to work with cache