-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyEnforcerCache.java
109 lines (93 loc) · 4.43 KB
/
PolicyEnforcerCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.enforcement;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CacheFactory;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyImport;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import scala.concurrent.ExecutionContextExecutor;
final class PolicyEnforcerCache implements Cache<PolicyId, Entry<PolicyEnforcer>> {
private final Cache<PolicyId, Entry<PolicyEnforcer>> delegate;
private final Map<PolicyId, Set<PolicyId>> policyIdToImportingMap;
PolicyEnforcerCache(final AsyncCacheLoader<PolicyId, Entry<PolicyEnforcer>> policyEnforcerCacheLoader,
final ExecutionContextExecutor cacheDispatcher,
final CacheConfig cacheConfig) {
policyIdToImportingMap = new ConcurrentHashMap<>();
this.delegate = CacheFactory.createCache(
(policyId, executor) -> policyEnforcerCacheLoader.asyncLoad(policyId, executor)
.whenComplete(((policyEnforcerEntry, throwable) -> policyEnforcerEntry.get()
.flatMap(PolicyEnforcer::getPolicy)
.map(Policy::getPolicyImports)
.filter(imports -> !imports.isEmpty())
.ifPresent(imports -> imports.stream()
.map(PolicyImport::getImportedPolicyId)
.forEach(importedPolicyId -> policyIdToImportingMap.compute(
importedPolicyId, (importedPolicyId1, importingPolicyIds) -> {
final Set<PolicyId> newImportingPolicyIds =
importingPolicyIds == null ? new HashSet<>() :
importingPolicyIds;
newImportingPolicyIds.add(policyId);
return newImportingPolicyIds;
}))))),
cacheConfig,
"policy_enforcer_cache",
cacheDispatcher
);
}
@Override
public CompletableFuture<Optional<Entry<PolicyEnforcer>>> get(final PolicyId key) {
return delegate.get(key);
}
@Override
public CompletableFuture<Optional<Entry<PolicyEnforcer>>> getIfPresent(final PolicyId key) {
return delegate.getIfPresent(key);
}
@Override
public Optional<Entry<PolicyEnforcer>> getBlocking(final PolicyId key) {
return delegate.getBlocking(key);
}
@Override
public boolean invalidate(final PolicyId policyId) {
// Invalidate the changed policy
final boolean directlyCached = delegate.invalidate(policyId);
// Invalidate all policies that import the changed policy
final boolean indirectlyCachedViaImport = Optional.ofNullable(policyIdToImportingMap.remove(policyId))
.stream()
.flatMap(Collection::stream)
.map(delegate::invalidate)
.reduce((previous, next) -> previous || next)
.orElse(false);
return directlyCached || indirectlyCachedViaImport;
}
@Override
public void put(final PolicyId key, final Entry<PolicyEnforcer> value) {
delegate.put(key, value);
}
@Override
public ConcurrentMap<PolicyId, Entry<PolicyEnforcer>> asMap() {
return delegate.asMap();
}
}