View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.pipeline.cache;
21  
22  import static java.util.concurrent.TimeUnit.DAYS;
23  
24  import java.util.Collection;
25  import java.util.EnumSet;
26  import java.util.concurrent.TimeUnit;
27  
28  import org.osgi.annotation.versioning.ProviderType;
29  
30  import io.wcm.caravan.io.http.request.CaravanHttpRequest;
31  import io.wcm.caravan.pipeline.JsonPipeline;
32  import io.wcm.caravan.pipeline.cache.spi.CacheAdapter;
33  
34  /**
35   * Default implementations of different cache strategies.
36   */
37  @ProviderType
38  public final class CacheStrategies {
39  
40    private static final EnumSet SUPPORTED_TIME_UNITS = EnumSet.of(TimeUnit.SECONDS, TimeUnit.MINUTES,
41        TimeUnit.HOURS, TimeUnit.DAYS);
42  
43    private CacheStrategies() {
44      // static methods only
45    }
46  
47    /**
48     * Invalidate item after a fixed time-to-live interval, using the same duration as storage time and
49     * revalidation-interval
50     * @param duration Time-to-live duration
51     * @param unit Time unit
52     * @return Cache strategy
53     */
54    public static CacheStrategy timeToLive(int duration, TimeUnit unit) {
55      int refreshInterval = toSeconds(duration, unit);
56      int storageTime = toSeconds(duration, unit);
57      return new CacheStrategy() {
58  
59        @Override
60        public CachePersistencyOptions getCachePersistencyOptions(Collection<CaravanHttpRequest> requests) {
61          return CachePersistencyOptions.createPersistentAndTimeToLive(refreshInterval, storageTime);
62        }
63      };
64    }
65  
66    /**
67     * Invalidate item after a time-to-idle interval: The content is considered immutable, and storage time will be
68     * extended to specified value on each get operation on this item, so it is kept in cache as long as it is requested
69     * @param duration Time-to-idle duration
70     * @param unit Time unit
71     * @return Cache strategy
72     */
73    public static CacheStrategy timeToIdle(int duration, TimeUnit unit) {
74      int refreshInterval = toSeconds(365, DAYS);
75      int storageTime = toSeconds(duration, unit);
76      return new CacheStrategy() {
77  
78        @Override
79        public CachePersistencyOptions getCachePersistencyOptions(Collection<CaravanHttpRequest> requests) {
80          return CachePersistencyOptions.createPersistentAndTimeToIdle(refreshInterval, storageTime);
81        }
82      };
83    }
84    
85    /**
86     * Invalidate item after a time-to-idle interval: The content is considered immutable, and storage time will be
87     * extended to specified value on each get operation on this item, so it is kept in cache as long as it is requested.
88     * Cache operations should be ignored by transient adapters and passed to the persistent adapters only.
89     * @param duration Time-to-idle duration
90     * @param unit Time unit
91     * @return Cache strategy
92     */
93    public static CacheStrategy nonTransientAndTimeToIdle(int duration, TimeUnit unit) {
94      int refreshInterval = toSeconds(365, DAYS);
95      int storageTime = toSeconds(duration, unit);
96      return new CacheStrategy() {
97  
98        @Override
99        public CachePersistencyOptions getCachePersistencyOptions(Collection<CaravanHttpRequest> requests) {
100         return new CachePersistencyOptions(refreshInterval, storageTime, true, false);
101       }
102     };
103   }
104 
105   /**
106    * Stores items only in the local, non-persistent {@link CacheAdapter} for the given maximum duration.
107    * @param duration maximum Time-to-live
108    * @param unit Time unit
109    * @return Cache strategy
110    */
111   public static CacheStrategy temporary(int duration, TimeUnit unit) {
112     int refreshInterval = toSeconds(duration, unit);
113     return new CacheStrategy() {
114 
115       @Override
116       public CachePersistencyOptions getCachePersistencyOptions(Collection<CaravanHttpRequest> requests) {
117         return CachePersistencyOptions.createTransient(refreshInterval);
118       }
119     };
120   }
121 
122   private static int toSeconds(int duration, TimeUnit unit) {
123     if (!SUPPORTED_TIME_UNITS.contains(unit)) {
124       throw new IllegalArgumentException("Unsupported time unit: " + unit);
125     }
126     long seconds = unit.toSeconds(duration);
127     if (seconds > Integer.MAX_VALUE) {
128       throw new IllegalArgumentException("Duration is too long: " + seconds + " seconds");
129     }
130     return (int)unit.toSeconds(duration);
131   }
132 
133   /**
134    * No caching. Can be used to disable caching in a {@link JsonPipeline} that already has some Cachepoints set.
135    * @return Cache strategy
136    */
137   public static CacheStrategy noCache() {
138     return new CacheStrategy() {
139 
140       @Override
141       public CachePersistencyOptions getCachePersistencyOptions(Collection<CaravanHttpRequest> requests) {
142         return CachePersistencyOptions.createTransient(0);
143       }
144     };
145   }
146 
147 
148 }