| 1 |
/** |
| 2 |
* Copyright (c) 2012-2017, jcabi.com |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: 1) Redistributions of source code must retain the above |
| 8 |
* copyright notice, this list of conditions and the following |
| 9 |
* disclaimer. 2) Redistributions in binary form must reproduce the above |
| 10 |
* copyright notice, this list of conditions and the following |
| 11 |
* disclaimer in the documentation and/or other materials provided |
| 12 |
* with the distribution. 3) Neither the name of the jcabi.com nor |
| 13 |
* the names of its contributors may be used to endorse or promote |
| 14 |
* products derived from this software without specific prior written |
| 15 |
* permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 19 |
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 |
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package com.jcabi.aspects.aj; |
| 31 |
|
| 32 |
import com.jcabi.aspects.Loggable; |
| 33 |
import com.jcabi.log.VerboseRunnable; |
| 34 |
import java.util.concurrent.BlockingQueue; |
| 35 |
import java.util.concurrent.ConcurrentMap; |
| 36 |
import java.util.concurrent.Executors; |
| 37 |
import java.util.concurrent.ScheduledExecutorService; |
| 38 |
import java.util.concurrent.TimeUnit; |
| 39 |
|
| 40 |
/** |
| 41 |
* For updating cache and cleaning expired cache. |
| 42 |
* @author Peng Chuanjiang ([email protected]) |
| 43 |
* @version $Id: 4c18b638735db6e83ba1da68a4d685c6e12aa222 $ |
| 44 |
*/ |
| 45 |
@SuppressWarnings("PMD.DoNotUseThreads") |
| 46 |
public final class UpdateMethodCacher { |
| 47 |
|
| 48 |
/** |
| 49 |
* Calling tunnels. |
| 50 |
*/ |
| 51 |
private final transient |
| 52 |
ConcurrentMap<MethodCacher.Key, MethodCacher.Tunnel> tunnels; |
| 53 |
|
| 54 |
/** |
| 55 |
* Service that cleans cache. |
| 56 |
*/ |
| 57 |
private final transient ScheduledExecutorService cleaner; |
| 58 |
|
| 59 |
/** |
| 60 |
* Save the keys of caches which need update. |
| 61 |
*/ |
| 62 |
private final transient BlockingQueue<MethodCacher.Key> updatekeys; |
| 63 |
|
| 64 |
/** |
| 65 |
* Service that update cache. |
| 66 |
*/ |
| 67 |
private final transient ScheduledExecutorService updater; |
| 68 |
|
| 69 |
/** |
| 70 |
* Public ctor. |
| 71 |
* @param tls ConcurrentMap |
| 72 |
* @param ukeys BlockingQueue |
| 73 |
*/ |
| 74 |
public UpdateMethodCacher( |
| 75 |
final ConcurrentMap<MethodCacher.Key, MethodCacher.Tunnel> tls, |
| 76 |
final BlockingQueue<MethodCacher.Key> ukeys) { |
| 77 |
this.tunnels = tls; |
| 78 |
this.updatekeys = ukeys; |
| 79 |
this.cleaner = Executors.newSingleThreadScheduledExecutor( |
| 80 |
new NamedThreads( |
| 81 |
"cacheable-clean", |
| 82 |
"automated cleaning of expired @Cacheable values" |
| 83 |
) |
| 84 |
); |
| 85 |
this.updater = Executors.newSingleThreadScheduledExecutor( |
| 86 |
new NamedThreads( |
| 87 |
"cacheable-update", |
| 88 |
"async update of expired @Cacheable values" |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Staring two threads(cheaner and updater) are used for |
| 95 |
* updating cache and cleaning expired cache. |
| 96 |
*/ |
| 97 |
public void start() { |
| 98 |
this.cleaner.scheduleWithFixedDelay( |
| 99 |
new VerboseRunnable( |
| 100 |
new Runnable() { |
| 101 |
@Override |
| 102 |
public void run() { |
| 103 |
UpdateMethodCacher.this.clean(); |
| 104 |
} |
| 105 |
} |
| 106 |
), |
| 107 |
1L, 1L, TimeUnit.SECONDS |
| 108 |
); |
| 109 |
this.updater.schedule( |
| 110 |
new VerboseRunnable( |
| 111 |
new Runnable() { |
| 112 |
@Override |
| 113 |
public void run() { |
| 114 |
UpdateMethodCacher.this.update(); |
| 115 |
} |
| 116 |
} |
| 117 |
), |
| 118 |
0L, TimeUnit.SECONDS |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Clean cache. |
| 124 |
*/ |
| 125 |
private void clean() { |
| 126 |
synchronized (this.tunnels) { |
| 127 |
for (final MethodCacher.Key key : this.tunnels.keySet()) { |
| 128 |
if (this.tunnels.get(key).expired() |
| 129 |
&& !this.tunnels.get(key).asyncUpdate()) { |
| 130 |
final MethodCacher.Tunnel tunnel = this.tunnels.remove(key); |
| 131 |
LogHelper.log( |
| 132 |
key.getLevel(), |
| 133 |
this, |
| 134 |
"%s:%s expired in cache", |
| 135 |
key, |
| 136 |
tunnel |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Update cache. |
| 145 |
*/ |
| 146 |
@SuppressWarnings("PMD.AvoidCatchingThrowable") |
| 147 |
private void update() { |
| 148 |
while (true) { |
| 149 |
try { |
| 150 |
final MethodCacher.Key key = this.updatekeys.take(); |
| 151 |
final MethodCacher.Tunnel tunnel = this.tunnels.get(key); |
| 152 |
if (tunnel != null && tunnel.expired()) { |
| 153 |
final MethodCacher.Tunnel newTunnel = tunnel.copy(); |
| 154 |
newTunnel.through(); |
| 155 |
this.tunnels.put(key, newTunnel); |
| 156 |
} |
| 157 |
} catch (final InterruptedException ex) { |
| 158 |
LogHelper.log( |
| 159 |
Loggable.ERROR, |
| 160 |
this, |
| 161 |
"%s:%s", |
| 162 |
ex.getMessage(), |
| 163 |
ex |
| 164 |
); |
| 165 |
// @checkstyle IllegalCatch (1 line) |
| 166 |
} catch (final Throwable ex) { |
| 167 |
LogHelper.log( |
| 168 |
Loggable.ERROR, |
| 169 |
this, |
| 170 |
"Exception message is %s, Exception is %s", |
| 171 |
ex.getMessage(), |
| 172 |
ex |
| 173 |
); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} |