View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 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.ehcache3.configuration;
35  
36  import info.magnolia.module.cache.filter.CachedEntry;
37  
38  import java.time.Duration;
39  import java.util.function.Supplier;
40  
41  import org.ehcache.expiry.ExpiryPolicy;
42  
43  /**
44   * {@link org.ehcache.expiry.Expiry} which gets TTL from the value object if it's instance of a {@link info.magnolia.module.cache.filter.CachedEntry} otherwise returns {@link Duration#INFINITE}.
45   * @param <K> the key type for the cache
46   * @param <V> the value type for the cache
47   */
48  public class EhCache3Expiry<K, V> implements ExpiryPolicy<K, V> {
49  
50      private Duration create = ExpiryPolicy.INFINITE;
51      private Duration access = null;
52      private Duration update = null;
53  
54      @Override
55      public Duration getExpiryForCreation(K key, V value) {
56          Duration duration = create;
57          if (value instanceof CachedEntry) {
58              int ttl = ((CachedEntry) value).getTimeToLiveInSeconds();
59              duration = ttl == -1 ? create : Duration.ofSeconds(ttl);
60          }
61          return duration;
62      }
63  
64      @Override
65      public Duration getExpiryForAccess(K key, Supplier<? extends V> value) {
66          return access;
67      }
68  
69      @Override
70      public Duration getExpiryForUpdate(K key, Supplier<? extends V> oldValue, V newValue) {
71          return update;
72      }
73  
74      public Long getCreate() {
75          return create == null ? null : create.getSeconds();
76      }
77  
78      public void setCreate(Long create) {
79          this.create = Duration.ofSeconds(create);
80      }
81  
82      public Long getAccess() {
83          return access == null ? null : access.getSeconds();
84      }
85  
86      public void setAccess(Long access) {
87          this.access = Duration.ofSeconds(access);
88      }
89  
90      public Long getUpdate() {
91          return update == null ? null : update.getSeconds();
92      }
93  
94      public void setUpdate(Long update) {
95          this.update = Duration.ofSeconds(update);
96      }
97  }