mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
added in memory cache and redis cache and marked them as application scoped
This commit is contained in:
22
pom.xml
22
pom.xml
@@ -19,6 +19,7 @@
|
|||||||
<jpa.datasource.name>java:openejb/Resource/My DataSource</jpa.datasource.name>
|
<jpa.datasource.name>java:openejb/Resource/My DataSource</jpa.datasource.name>
|
||||||
<jpa.dialect>org.hibernate.dialect.HSQLDialect</jpa.dialect>
|
<jpa.dialect>org.hibernate.dialect.HSQLDialect</jpa.dialect>
|
||||||
<jpa.cache>false</jpa.cache>
|
<jpa.cache>false</jpa.cache>
|
||||||
|
<cache_service.class>com.commafeed.backend.cache.InMemoryCacheService</cache_service.class>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
@@ -69,6 +70,16 @@
|
|||||||
<version>2.3</version>
|
<version>2.3</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
<webResources>
|
||||||
|
<resource>
|
||||||
|
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
|
||||||
|
<targetPath>WEB-INF</targetPath>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
<includes>
|
||||||
|
<include>**/beans.xml</include>
|
||||||
|
</includes>
|
||||||
|
</resource>
|
||||||
|
</webResources>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -172,6 +183,11 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>redis.clients</groupId>
|
||||||
|
<artifactId>jedis</artifactId>
|
||||||
|
<version>2.0.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.liquibase</groupId>
|
<groupId>org.liquibase</groupId>
|
||||||
<artifactId>liquibase-core</artifactId>
|
<artifactId>liquibase-core</artifactId>
|
||||||
@@ -443,6 +459,12 @@
|
|||||||
<jpa.cache>true</jpa.cache>
|
<jpa.cache>true</jpa.cache>
|
||||||
</properties>
|
</properties>
|
||||||
</profile>
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>redis</id>
|
||||||
|
<properties>
|
||||||
|
<cache_service.class>com.commafeed.backend.cache.RedisCacheService</cache_service.class>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
<profile>
|
<profile>
|
||||||
<id>mysql</id>
|
<id>mysql</id>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -1,31 +1,21 @@
|
|||||||
package com.commafeed.backend.cache;
|
package com.commafeed.backend.cache;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.commafeed.backend.model.FeedEntry;
|
import com.commafeed.backend.model.FeedEntry;
|
||||||
import com.google.common.cache.Cache;
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
|
||||||
|
|
||||||
public class CacheService {
|
public abstract class CacheService {
|
||||||
|
|
||||||
Cache<String, Marker> entryCache = CacheBuilder.newBuilder()
|
public abstract List<String> getLastEntries(Feed feed);
|
||||||
.maximumSize(1000000).expireAfterWrite(24, TimeUnit.HOURS).build();
|
|
||||||
|
|
||||||
private static enum Marker {
|
public abstract void setLastEntries(Feed feed, List<String> entries);
|
||||||
INSTANCE
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasFeedEntry(Feed feed, FeedEntry entry) {
|
public String buildKey(Feed feed, FeedEntry entry) {
|
||||||
return entryCache.getIfPresent(buildKey(feed, entry)) == Marker.INSTANCE;
|
return DigestUtils.sha1Hex(entry.getGuid() +
|
||||||
}
|
|
||||||
|
|
||||||
public void putFeedEntry(Feed feed, FeedEntry entry) {
|
|
||||||
entryCache.put(buildKey(feed, entry), Marker.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String buildKey(Feed feed, FeedEntry entry) {
|
|
||||||
return String.format("%s:%s:%s", feed.getId(), entry.getGuid(),
|
|
||||||
entry.getUrl());
|
entry.getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
46
src/main/java/com/commafeed/backend/cache/InMemoryCacheService.java
vendored
Normal file
46
src/main/java/com/commafeed/backend/cache/InMemoryCacheService.java
vendored
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package com.commafeed.backend.cache;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.enterprise.inject.Alternative;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.Feed;
|
||||||
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
|
||||||
|
@Alternative
|
||||||
|
@ApplicationScoped
|
||||||
|
public class InMemoryCacheService extends CacheService {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ApplicationSettingsService applicationSettingsService;
|
||||||
|
|
||||||
|
private Cache<Long, List<String>> entryCache;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
int capacity = applicationSettingsService.get().isHeavyLoad() ? 1000000 : 100;
|
||||||
|
entryCache = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(capacity).expireAfterWrite(24, TimeUnit.HOURS).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getLastEntries(Feed feed) {
|
||||||
|
List<String> list = entryCache.getIfPresent(feed.getId());
|
||||||
|
if (list == null) {
|
||||||
|
list = Collections.emptyList();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLastEntries(Feed feed, List<String> entries) {
|
||||||
|
entryCache.put(feed.getId(), entries);
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/main/java/com/commafeed/backend/cache/RedisCacheService.java
vendored
Normal file
62
src/main/java/com/commafeed/backend/cache/RedisCacheService.java
vendored
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package com.commafeed.backend.cache;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.enterprise.inject.Alternative;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
import redis.clients.jedis.Pipeline;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.Feed;
|
||||||
|
import com.google.api.client.util.Lists;
|
||||||
|
|
||||||
|
@Alternative
|
||||||
|
@ApplicationScoped
|
||||||
|
public class RedisCacheService extends CacheService {
|
||||||
|
|
||||||
|
private JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getLastEntries(Feed feed) {
|
||||||
|
List<String> list = Lists.newArrayList();
|
||||||
|
Jedis jedis = pool.getResource();
|
||||||
|
try {
|
||||||
|
String key = buildKey(feed);
|
||||||
|
Set<String> members = jedis.smembers(key);
|
||||||
|
for (String member : members) {
|
||||||
|
list.add(member);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLastEntries(Feed feed, List<String> entries) {
|
||||||
|
Jedis jedis = pool.getResource();
|
||||||
|
try {
|
||||||
|
String key = buildKey(feed);
|
||||||
|
|
||||||
|
Pipeline pipe = jedis.pipelined();
|
||||||
|
pipe.del(key);
|
||||||
|
for (String entry : entries) {
|
||||||
|
pipe.sadd(key, entry);
|
||||||
|
}
|
||||||
|
pipe.expire(key, (int) TimeUnit.HOURS.toSeconds(24));
|
||||||
|
pipe.sync();
|
||||||
|
} finally {
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildKey(Feed feed) {
|
||||||
|
return "feed:" + feed.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,6 +10,8 @@ import javax.inject.Inject;
|
|||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.apache.commons.lang3.time.DateUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.commafeed.backend.MetricsBean;
|
import com.commafeed.backend.MetricsBean;
|
||||||
import com.commafeed.backend.dao.FeedDAO;
|
import com.commafeed.backend.dao.FeedDAO;
|
||||||
@@ -21,6 +23,8 @@ import com.google.common.collect.Queues;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class FeedRefreshTaskGiver {
|
public class FeedRefreshTaskGiver {
|
||||||
|
|
||||||
|
protected static final Logger log = LoggerFactory.getLogger(FeedRefreshTaskGiver.class);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
FeedDAO feedDAO;
|
FeedDAO feedDAO;
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import com.commafeed.backend.model.FeedSubscription;
|
|||||||
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
||||||
import com.commafeed.backend.services.ApplicationSettingsService;
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||||
import com.commafeed.backend.services.FeedUpdateService;
|
import com.commafeed.backend.services.FeedUpdateService;
|
||||||
|
import com.google.api.client.util.Lists;
|
||||||
import com.google.common.util.concurrent.Striped;
|
import com.google.common.util.concurrent.Striped;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@@ -141,9 +142,21 @@ public class FeedRefreshUpdater {
|
|||||||
if (entries.isEmpty() == false) {
|
if (entries.isEmpty() == false) {
|
||||||
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
||||||
.findByFeed(feed);
|
.findByFeed(feed);
|
||||||
|
List<String> lastEntries = cache.getLastEntries(feed);
|
||||||
|
List<String> currentEntries = Lists.newArrayList();
|
||||||
for (FeedEntry entry : entries) {
|
for (FeedEntry entry : entries) {
|
||||||
ok &= updateEntry(feed, entry, subscriptions);
|
String cacheKey = cache.buildKey(feed, entry);
|
||||||
|
if (!lastEntries.contains(cacheKey)) {
|
||||||
|
log.debug("cache miss for {}", entry.getUrl());
|
||||||
|
ok &= updateEntry(feed, entry, subscriptions);
|
||||||
|
metricsBean.entryCacheMiss();
|
||||||
|
} else {
|
||||||
|
log.debug("cache hit for {}", entry.getUrl());
|
||||||
|
metricsBean.entryCacheHit();
|
||||||
|
}
|
||||||
|
currentEntries.add(cacheKey);
|
||||||
}
|
}
|
||||||
|
cache.setLastEntries(feed, currentEntries);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (applicationSettingsService.get().isPubsubhubbub()) {
|
if (applicationSettingsService.get().isPubsubhubbub()) {
|
||||||
@@ -171,15 +184,7 @@ public class FeedRefreshUpdater {
|
|||||||
try {
|
try {
|
||||||
locked = lock.tryLock(1, TimeUnit.MINUTES);
|
locked = lock.tryLock(1, TimeUnit.MINUTES);
|
||||||
if (locked) {
|
if (locked) {
|
||||||
if (!cache.hasFeedEntry(feed, entry)) {
|
feedUpdateService.updateEntry(feed, entry, subscriptions);
|
||||||
log.debug("cache miss for {}", entry.getUrl());
|
|
||||||
feedUpdateService.updateEntry(feed, entry, subscriptions);
|
|
||||||
cache.putFeedEntry(feed, entry);
|
|
||||||
metricsBean.entryCacheMiss();
|
|
||||||
} else {
|
|
||||||
log.debug("cache hit for {}", entry.getUrl());
|
|
||||||
metricsBean.entryCacheHit();
|
|
||||||
}
|
|
||||||
success = true;
|
success = true;
|
||||||
} else {
|
} else {
|
||||||
log.error("lock timeout for " + feed.getUrl() + " - " + key);
|
log.error("lock timeout for " + feed.getUrl() + " - " + key);
|
||||||
|
|||||||
@@ -2,5 +2,7 @@
|
|||||||
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="
|
xsi:schemaLocation="
|
||||||
http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/beans_1_0.xsd">
|
http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/beans_1_0.xsd">
|
||||||
|
<alternatives>
|
||||||
|
<class>${cache_service.class}</class>
|
||||||
|
</alternatives>
|
||||||
</beans>
|
</beans>
|
||||||
Reference in New Issue
Block a user