Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fetching object tags from unsaved object produces error. Updated READ…

…ME. Added classes to help caching private data along authenticated users. Updated package.xml
  • Loading branch information...
commit 017d2ce42226ad89f36ca8a857c7032327d5bac7 1 parent c2ddaee
@fruit authored
View
60 README.markdown
@@ -129,6 +129,10 @@ Then rebuild your models:
actAs:
Cachetaggable: ~
+And don't forget to rebuild models again:
+
+ $ ./symfony doctrine:build-model
+
## 5. Enable cache and declare required helpers in ``/apps/%APP%/config/settings.yml``:
dev:
@@ -294,7 +298,7 @@ Then rebuild your models:
with_layout: false
enabled: true
-## How to cache Doctrine_Records/Doctrine_Collections?
+## How to cache ``Doctrine_Record``/``Doctrine_Collection``?
* Does not depends on ``cache.yml`` file
@@ -324,6 +328,46 @@ Then rebuild your models:
_NB. Please read "<a href="#quick-setup">Quick setup</a>" before reading this._
+## How to cache private blocks (actions/pages/partials) for authenticated users
+
+ Symfony's cache mechanism creates the unique key to each block you want to cache based on
+ following arguments:
+
+ - Module name
+ - Action name
+ - $_GET arguments
+
+ In case you would like to cache user's private data you must be very careful.
+ To prevent users of seeing other user private data you need to add
+ additional parameter to distinguish cached blocks among other private blocks.
+
+ The easiest way is to keep user_id/username in URL, but it's awful.
+ I suggest to add custom $_GET parameter on the fly. This will
+ prevent of showing "user_id" in URL.
+
+ What should you do is to register a new filter ``AuthParamFilter`` and switch standard
+ ``sfWebRequest`` with plugin's one ``sfCacheTaggingWebRequest``.
+
+ Place ``AuthParamFilter`` before "caching" filter in ``apps/%application_name%/config/filters.yml``
+
+ rendering: ~
+ security: ~
+
+ auth_params:
+ class: AuthParamFilter
+
+ cache: ~
+ execution: ~
+
+ Switch to sfCacheTaggingWebRequest in ``apps/%application_name%/config/factories.yml``
+
+ all:
+ request:
+ class: sfCacheTaggingWebRequest
+
+ That's all. Now cache content will be based on additional parameter "user_id" in case
+ user have successfully authenticated.
+
## Explaining ``/config/factories.yml``
all:
@@ -590,6 +634,20 @@ Component example:
}
}
+
+## Why you might want to unset collection tag from list of existing cache tags
+
+Imagine following situation - you want to cache article
+"Oil is discovered in Saudi Arabia" for 3rd March of 1938.
+
+You know it makes no sense if someone had removed another article or had
+added new one.
+
+Collection tag is used to identify whether this article should be invalidated
+in case new article is added or some other article is removed
+
+TODO: Why do we need collection tag when fetching only ony object with findOneById()?
+
## Configurating Doctrine`s query cache
Remember to enable Doctrine query cache in production:
View
43 lib/filter/AuthParamFilter.class.php
@@ -0,0 +1,43 @@
+<?php
+
+ /*
+ * This file is part of the sfCacheTaggingPlugin package.
+ * (c) 2009-2011 Ilya Sabelnikov <fruit.dev@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+ /**
+ * Adds additional $_GET parameter to allow caching authenticated
+ * private data.
+ * Symfony cache block key will be based on "user_id" argument too.
+ *
+ * @package sfCacheTaggingPlugin
+ * @subpackage filters
+ * @author Ilya Sabelnikov <fruit.dev@gmail.com>
+ */
+ class AuthParamFilter extends sfFilter
+ {
+ public function execute ($filterChain)
+ {
+ $context = $this->getContext();
+
+ if ($this->isFirstCall())
+ {
+ if ($context->getUser()->isAuthenticated())
+ {
+ $context
+ ->getRequest()
+ ->addGetParameters(
+ array(
+ 'user_id' => $context->getUser()->getId(),
+ )
+ )
+ ;
+ }
+ }
+
+ $filterChain->execute();
+ }
+ }
View
60 lib/request/sfCacheTaggingWebRequest.class.php
@@ -0,0 +1,60 @@
+<?php
+
+ /*
+ * This file is part of the sfCacheTaggingPlugin package.
+ * (c) 2009-2011 Ilya Sabelnikov <fruit.dev@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+ /**
+ * Custom HTTP Request class to access the encapsulated
+ * private variable "$this->getParameters"
+ *
+ * @package sfCacheTaggingPlugin
+ * @subpackage request
+ * @author Ilya Sabelnikov <fruit.dev@gmail.com>
+ */
+ class sfCacheTaggingWebRequest extends sfWebRequest
+ {
+ /**
+ * Appends new _GET parameters to the existing.
+ * Used in AuthParamFilter to add custom parameters on the fly.
+ *
+ * @var array $params
+ * @return sfCacheTaggingWebRequest
+ */
+ public function addGetParameters ($params)
+ {
+ $this->getParameters = array_merge($this->getParameters, $params);
+
+ return $this;
+ }
+
+ /**
+ * Deletes all registered _GET parameters.
+ *
+ * Used to cache something without depending on whether user is
+ * authenticated or not. All users should see the same content.
+ * Real life situation: Page with "Terms & Conditions"
+ */
+ public function deleteGetParameters ()
+ {
+ $this->getParameters = array();
+ }
+
+ /**
+ * Return only _GET parameters + cleaning cache attribute for
+ * authorized users @see AuthParamFilter
+ *
+ * Used when page contains listing with filter (sfFormFilter) and
+ * Pagination/Sorting should be avoided from custom "user_id" parameters.
+ *
+ * @return array
+ */
+ public function getFilteredGetParameters ()
+ {
+ return array_diff_key($this->getParameters, array('user_id' => null));
+ }
+ }
View
5 lib/vendor/Doctrine/Template/Cachetaggable.php
@@ -149,6 +149,11 @@ public function getCacheTags ($deep = true)
{
continue;
}
+
+ if ($reference instanceof Doctrine_Record && ! $reference->exists())
+ {
+ continue;
+ }
$table = $reference->getTable();
View
38 package.xml
@@ -22,10 +22,10 @@
<email>fruit.dev@gmail.com</email>
<active>yes</active>
</lead>
- <date>2011-10-20</date>
- <time>16:30:00</time>
+ <date>2011-12-06</date>
+ <time>21:40:00</time>
<version>
- <release>4.1.1</release>
+ <release>4.2.0</release>
<api>4.0.1</api>
</version>
<stability>
@@ -34,8 +34,9 @@
</stability>
<license uri="http://www.symfony-project.com/license">MIT license</license>
<notes>
- * ilya: [removed] Test environment files removed from package. All tests available only from GIT repository. [GH-15]
- * ilya: [updated] README
+ * ilya: [fixed] Attaching unsaved record to cache block cause the error
+ * ilya: [added] Added custom HTTP request class and custom filter to differ cache blocks between authenticated users
+ * ilya: [updated] README - added notes how to keep different cache to each authenticated user
</notes>
<contents>
<dir name="/">
@@ -44,7 +45,7 @@
<file name="sfCacheTaggingPluginConfiguration.class.php" md5sum="5c5fca78d6de507e3aabda7ed6b641e7" role="data" />
</dir>
<file name="LICENSE" md5sum="d840195351e168d3bc3ab9b626617907" role="data" />
- <file name="README" md5sum="49a01f35f05993f9602431b2fcbc01f4" role="data" />
+ <file name="README" md5sum="b2aee2cc82c666d9e80957e90323be70" role="data" />
<dir name="lib">
<dir name="util">
<file name="sfCacheTaggingToolkit.class.php" md5sum="035bc2137475cbc2f5354dd664ee4d88" role="data" />
@@ -53,6 +54,9 @@
<file name="sfTagNamespacedParameterHolder.class.php" md5sum="126fcaf02e158b87e733cbe42e72c2f6" role="data" />
<file name="sfViewCacheTagManagerBridge.class.php" md5sum="c8e1eb30d4e72fc3adce96deebe8d238" role="data" />
</dir>
+ <dir name="request">
+ <file name="sfCacheTaggingWebRequest.class.php" md5sum="75155dd100eb88aeb5a0ee32694828cf" role="data" />
+ </dir>
<dir name="log">
<file name="sfNoCacheTagLogger.class.php" md5sum="b57a946d753a714dd1da7ce72014f9ed" role="data" />
<file name="sfCacheTagLogger.class.php" md5sum="b11e6ff69fce8d8100e6fa52d823b316" role="data" />
@@ -64,6 +68,9 @@
<dir name="view">
<file name="sfViewCacheTagManager.class.php" md5sum="46aa91fb0cd8eafa3d10a0c5dda27f57" role="data" />
</dir>
+ <dir name="filter">
+ <file name="AuthParamFilter.class.php" md5sum="e59b84f5e32b3a188b1de826b8d9fe8e" role="data" />
+ </dir>
<dir name="exception">
<file name="sfCacheDisabledException.class.php" md5sum="589a2a27a80a1ff1a90d8f20968e21e1" role="data" />
<file name="sfCacheMissingContextException.class.php" md5sum="b396e6ef4ad273bdf59226c4aff2a6d4" role="data" />
@@ -95,7 +102,7 @@
<dir name="Listener">
<file name="Cachetaggable.php" md5sum="bb4728b8b2b529634c8f8f10c632522b" role="data" />
</dir>
- <file name="Cachetaggable.php" md5sum="67d4aaf7b61d26d919f69662c93bf2f8" role="data" />
+ <file name="Cachetaggable.php" md5sum="fde8c06415b09772d1528259a6a1c704" role="data" />
</dir>
<dir name="Cache">
<file name="Proxy.php" md5sum="027c16edf4e53ebd6c0b7296cf8c1eec" role="data" />
@@ -132,6 +139,23 @@
<changelog>
<release>
<version>
+ <release>4.2.0</release>
+ <api>4.0.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2011-12-06</date>
+ <license uri="http://www.symfony-project.com/license">MIT license</license>
+ <notes>
+ * ilya: [fixed] Attaching unsaved record to cache block cause the error
+ * ilya: [added] Added custom HTTP request class and custom filter to differ cache blocks between authenticated users
+ * ilya: [updated] README - added notes how to keep different cache to each authenticated user
+ </notes>
+ </release>
+ <release>
+ <version>
<release>4.1.1</release>
<api>4.0.1</api>
</version>
Please sign in to comment.
Something went wrong with that request. Please try again.