Package: DefaultMetadataCache
DefaultMetadataCache
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultMetadataCache() |
|
|
|
|
|
||||||||||||||||||||
findMetadataById(Id, ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
getClock() |
|
|
|
|
|
||||||||||||||||||||
getMetadataExpirationTime() |
|
|
|
|
|
||||||||||||||||||||
setClock(Supplier) |
|
|
|
|
|
||||||||||||||||||||
setMetadataExpirationTime(int) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.media.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.inject.Inject;
32: import java.time.Clock;
33: import java.time.ZonedDateTime;
34: import java.util.HashMap;
35: import java.util.Map;
36: import java.util.function.Supplier;
37: import java.io.IOException;
38: import it.tidalwave.util.Id;
39: import it.tidalwave.util.NotFoundException;
40: import it.tidalwave.northernwind.core.model.ResourceProperties;
41: import lombok.Getter;
42: import lombok.RequiredArgsConstructor;
43: import lombok.Setter;
44: import lombok.ToString;
45: import lombok.extern.slf4j.Slf4j;
46:
47: /***********************************************************************************************************************
48: *
49: * A default implementation of {@link MetadataCache}.
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: @Slf4j
55: public class DefaultMetadataCache implements MetadataCache
56: {
57: /*******************************************************************************************************************
58: *
59: * A holder of {@code Metadata} together with expiration information.
60: *
61: ******************************************************************************************************************/
62: @RequiredArgsConstructor @Getter @ToString
63: class ExpirableMetadata
64: {
65: @Nonnull
66: private final Metadata metadata;
67:
68: private final ZonedDateTime creationTime = ZonedDateTime.now(clock.get());
69:
70: @Nonnull
71: private ZonedDateTime expirationTime = creationTime.plusSeconds(metadataExpirationTime);
72:
73: /***************************************************************************************************************
74: *
75: * Postpones the expiration time.
76: *
77: **************************************************************************************************************/
78: public void postponeExpirationTime()
79: {
80: expirationTime = ZonedDateTime.now(clock.get()).plusSeconds(metadataExpirationTime);
81: }
82: }
83:
84: public static final int DEFAULT_METADATA_EXPIRATION_TIME = 10 * 60;
85:
86: @Getter @Setter @Nonnull
87: private Supplier<Clock> clock = Clock::systemDefaultZone;
88:
89: /** Expiration time for metadata in seconds; after this time, metadata are reloaded. */
90: @Getter @Setter @Nonnegative
91: private int metadataExpirationTime = DEFAULT_METADATA_EXPIRATION_TIME;
92:
93: @Inject
94: private MetadataLoader metadataLoader;
95:
96: /* package */ final Map<Id, ExpirableMetadata> metadataMapById = new HashMap<>();
97:
98: /*******************************************************************************************************************
99: *
100: * {@inheritDoc}
101: *
102: ******************************************************************************************************************/
103: // FIXME: shouldn't synchronize the whole method, only map manipulation
104: @Override @Nonnull
105: public synchronized Metadata findMetadataById (@Nonnull final Id mediaId,
106: @Nonnull final ResourceProperties siteNodeProperties)
107: throws NotFoundException, IOException
108: {
109: log.debug("findMetadataById({}, ...)", mediaId);
110: var metadata = metadataMapById.get(mediaId);
111:
112:• if ((metadata != null) && metadata.getExpirationTime().isAfter(ZonedDateTime.now(clock.get())))
113: {
114: log.debug(">>>> returning cached data which will expire at {}", metadata.getExpirationTime());
115: return metadata.getMetadata();
116: }
117:
118: final var file = metadataLoader.findMediaResourceFile(siteNodeProperties, mediaId);
119:
120:• if (metadata != null)
121: {
122: final var fileLatestModificationTime = file.getLatestModificationTime();
123: final var metadataCreationTime = metadata.getCreationTime();
124:
125:• if (fileLatestModificationTime.isAfter(metadataCreationTime))
126: {
127: log.debug(">>>>>>>> expiring metadata: file {} > metadata {}",
128: fileLatestModificationTime, metadataCreationTime);
129: metadata = null;
130: }
131: else
132: {
133: log.debug(">>>>>>>> postponing metadata expiration: file {} < metadata {}",
134: fileLatestModificationTime, metadataCreationTime);
135: metadata.postponeExpirationTime();
136: }
137: }
138:
139:• if (metadata == null)
140: {
141: log.debug(">>>> loading metadata...");
142: metadata = new ExpirableMetadata(metadataLoader.loadMetadata(file));
143: metadataMapById.put(mediaId, metadata);
144: }
145:
146: return metadata.getMetadata();
147: }
148: }