initial ehcache support

This commit is contained in:
Athou
2013-06-05 11:58:35 +02:00
parent b99a7fb14a
commit 7cbcf1080b
15 changed files with 106 additions and 8 deletions

18
pom.xml
View File

@@ -21,6 +21,7 @@
</properties>
<repositories>
<repository>
<id>hsqldb.snapshots</id>
<url>http://www.hsqldb.org/repos/</url>
@@ -99,6 +100,10 @@
<lib>org.hibernate.common:hibernate-commons-annotations:4.0.1.Final</lib>
<lib>org.hibernate:hibernate-validator:4.3.1.Final</lib>
<lib>org.jboss.logging:jboss-logging:3.1.3.GA</lib>
<lib>net.sf.ehcache:ehcache-core:2.6.6</lib>
<lib>org.hibernate:hibernate-ehcache:4.1.11.Final</lib>
<lib>dom4j:dom4j:1.6.1</lib>
<lib>antlr:antlr:2.7.7</lib>
<lib>remove:openjpa-</lib>
@@ -178,6 +183,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.1.11.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>uaihebert.com</groupId>
<artifactId>EasyCriteria</artifactId>

View File

@@ -1,10 +1,20 @@
package com.commafeed.backend;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.ejb.EntityManagerImpl;
import org.hibernate.stat.Statistics;
@Singleton
public class MetricsBean {
@PersistenceContext
EntityManager em;
private Metric lastMinute = new Metric();
private Metric thisMinute = new Metric();
@@ -61,6 +71,14 @@ public class MetricsBean {
return lastHour;
}
public String getCacheStats() {
EntityManagerImpl impl = (EntityManagerImpl) em.getDelegate();
Session session = impl.getSession();
SessionFactory sessionFactory = session.getSessionFactory();
Statistics statistics = sessionFactory.getStatistics();
return statistics.toString();
}
public static class Metric {
private int feedsRefreshed;
private int feedsUpdated;

View File

@@ -3,6 +3,7 @@ package com.commafeed.backend.model;
import java.util.Date;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -13,6 +14,8 @@ import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
import com.google.common.collect.Sets;
@@ -22,6 +25,8 @@ import com.google.common.collect.Sets;
@org.hibernate.annotations.Table(appliesTo = "FEEDS", indexes = { @Index(name = "disabled_lastupdated_index", columnNames = {
"disabledUntil", "lastUpdated" }), })
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Feed extends AbstractModel {
/**

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.model;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -10,11 +11,16 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.google.common.collect.Sets;
@Entity
@Table(name = "FEEDCATEGORIES")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FeedCategory extends AbstractModel {
@Column(length = 128, nullable = false)

View File

@@ -3,6 +3,7 @@ package com.commafeed.backend.model;
import java.util.Date;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -17,6 +18,8 @@ import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
import com.google.api.client.util.Sets;
@@ -24,6 +27,8 @@ import com.google.api.client.util.Sets;
@Entity
@Table(name = "FEEDENTRIES")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FeedEntry extends AbstractModel {
@Column(length = 2048, nullable = false)

View File

@@ -1,5 +1,6 @@
package com.commafeed.backend.model;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -7,9 +8,14 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "FEEDENTRYSTATUSES")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FeedEntryStatus extends AbstractModel {
@ManyToOne(fetch = FetchType.LAZY)

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.model;
import java.util.Date;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -11,11 +12,15 @@ import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
@Entity
@Table(name = "FEEDPUSHINFOS")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FeedPushInfo extends AbstractModel {
@JoinColumn(unique = true)

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.model;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -11,9 +12,14 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "FEEDSUBSCRIPTIONS")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FeedSubscription extends AbstractModel {
@ManyToOne(fetch = FetchType.LAZY)

View File

@@ -3,6 +3,7 @@ package com.commafeed.backend.model;
import java.util.Date;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -11,6 +12,8 @@ import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
import com.google.common.collect.Sets;
@@ -18,6 +21,8 @@ import com.google.common.collect.Sets;
@Entity
@Table(name = "USERS")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends AbstractModel {
@Column(length = 32, nullable = false, unique = true)

View File

@@ -1,5 +1,6 @@
package com.commafeed.backend.model;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
@@ -9,9 +10,14 @@ import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "USERROLES")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UserRole extends AbstractModel {
public static enum Role {

View File

@@ -1,5 +1,6 @@
package com.commafeed.backend.model;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
@@ -10,9 +11,14 @@ import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "USERSETTINGS")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UserSettings extends AbstractModel {
public enum ReadingMode {

View File

@@ -85,8 +85,8 @@ public class SubscriptionHandler {
}
log.debug("subscribed to {} for {}", hub, topic);
} catch (Exception e) {
log.error("Could not subscribe to {} for {} : {}", hub, topic,
e.getMessage(), e);
log.error("Could not subscribe to {} for {} : " + e.getMessage(),
hub, topic);
} finally {
client.getConnectionManager().shutdown();
}

View File

@@ -21,7 +21,6 @@ import com.commafeed.frontend.SecurityCheck;
import com.commafeed.frontend.model.UserModel;
import com.commafeed.frontend.model.request.IDRequest;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.wordnik.swagger.annotations.Api;
@@ -176,11 +175,13 @@ public class AdminREST extends AbstractResourceREST {
@Path("/metrics")
@GET
public Response getMetrics() {
Map<String, ? extends Object> map = ImmutableMap.of("lastMinute",
metricsBean.getLastMinute(), "lastHour",
metricsBean.getLastHour(), "backlog",
feedDAO.getUpdatableCount(), "queue",
feedRefreshUpdater.getQueueSize());
Map<String, Object> map = Maps.newLinkedHashMap();
map.put("lastMinute", metricsBean.getLastMinute());
map.put("lastHour", metricsBean.getLastHour());
map.put("backlog", feedDAO.getUpdatableCount());
map.put("queue", feedRefreshUpdater.getQueueSize());
map.put("cache", metricsBean.getCacheStats());
return Response.ok(map).build();
}
}

View File

@@ -7,11 +7,20 @@
<persistence-unit name="primary">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>${jpa.datasource.name}</jta-data-source>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="${jpa.show_sql}" />
<property name="hibernate.dialect" value="${jpa.dialect}" />
<property name="hibernate.default_batch_fetch_size" value="100" />
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.generate_statistics" value="true" />
</properties>
</persistence-unit>
</persistence>

View File

@@ -1,6 +1,8 @@
log4j.logger.com.commafeed=INFO, CONSOLE
log4j.logger.org=WARN, CONSOLE
log4j.logger.ro=WARN, CONSOLE
log4j.logger.net.sf.ehcache=ALL, CONSOLE
log4j.logger.org.hibernate.cache=ALL, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout