View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.module.cache.filter.CacheResponseWrapper;
38  import info.magnolia.voting.voters.VoterSet;
39  
40  /**
41   * The CachePolicy determines is a requested page should be cached,
42   * retrieved from the cache or not cached at all.
43   * It is called for every request and should thus also take care of
44   * any expiration policy - i.e if the page should be re-cached.
45   *
46   * The CacheFilter (or any other client component) can determine its
47   * behaviour based on the return CachePolicyResult, which holds both
48   * the behaviour to take and the cache key to use when appropriate.
49   *
50   * @author gjoseph
51   * @version $Revision: $ ($Author: $)
52   */
53  public interface CachePolicy {
54  
55      /**
56       * Implementations can chose whether to cache or not - but note that the
57       * aggregationState might not be completely populated. Every request should be
58       * cacheable, not only those processed through Magnolia's RenderingFilter.
59       */
60      CachePolicyResult shouldCache(final Cache cache, final AggregationState aggregationState, final FlushPolicy flushPolicy);
61  
62      /**
63       * Returns cache key for the given item or null if such key can't be obtained or policy doesn't want to share it.
64       * When using aggregationState, key is expected to be composed with aggregated request in mind and policy has to choose only
65       * one such key denoting item to return to the client, if such an item indeed exists and can be served.
66       * When not existing key might be used to store the cache entry. Since only one version of the content can be streamed back to
67       * the client, it makes sense to create only one cache entry for the such a key as well.
68       */
69      Object retrieveCacheKey(final AggregationState aggregationState);
70  
71  
72      /**
73       * Returns cache keys for the given item or null if such keys can't be obtained or policy doesn't want to share it. Since in
74       * this case uuid is used to retrieve the cache key, it is quite possible that multiple different representations of the content
75       * denoted by uuid exist and all such keys should be returned leaving it up to requesting object to deal with such a multiplicity.
76       * Please note that returned keys might not be necessary multiple representations of the content denoted by provided uuid, but
77       * also quite possibly all the content deemed related or partially used to construct any of the returned cache keys.
78       */
79      Object[] retrieveCacheKeys(final String uuid, final String repository);
80  
81      /**
82       * Persists mapping between uuid and cache key in case the given cache policy implementation cares about such details.
83       */
84      void persistCacheKey(String repo, String uuid, Object key);
85  
86      /**
87       * Returns cache keys for the given item or null if such keys can't be obtained or policy doesn't want to share it. Since in
88       * this case uuid is used to retrieve the cache key, it is quite possible that multiple different representations of the content
89       * denoted by uuid exist and all such keys should be returned leaving it up to requesting object to deal with such a multiplicity.
90       * Please note that returned keys might not be necessary multiple representations of the content denoted by provided uuid, but
91       * also quite possibly all the content deemed related or partially used to construct any of the returned cache keys.
92       * At the call to this method, cache policy should not only return the keys associated with the uuid, but also remove all returned uuid-cahce key mappings.
93       */
94      Object[] removeCacheKeys(String uuid, String repository);
95  
96  
97      VoterSet<CacheResponseWrapper> getTtlVoters();
98  }