Skip to content

Commit

Permalink
fix: add caching to HibernateNewsStore (#375)
Browse files Browse the repository at this point in the history
* fix: add caching to HibernateNewsStore

* fix: get newsset after creating for pk
  • Loading branch information
bjagg authored Mar 13, 2023
1 parent 3dec2b7 commit cdf6e74
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import org.jasig.portlet.newsreader.PredefinedNewsConfiguration;
import org.jasig.portlet.newsreader.PredefinedNewsDefinition;
import org.jasig.portlet.newsreader.UserDefinedNewsConfiguration;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.springframework.cache.annotation.Cacheable;

/**
* HibernateNewsStore provides a hibernate implementation of the NewsStore.
*
Expand Down Expand Up @@ -277,6 +280,7 @@ public void deleteNewsDefinition(PredefinedNewsDefinition definition) {
}
}

@Cacheable("HibernateNewsStore.userRoles")
public List<String> getUserRoles() {
try {

Expand All @@ -291,6 +295,7 @@ public List<String> getUserRoles() {
}
}

@Cacheable("HibernateNewsStore.newsSetById")
public NewsSet getNewsSet(Long id) {

try {
Expand All @@ -303,6 +308,7 @@ public NewsSet getNewsSet(Long id) {

}

@Cacheable("HibernateNewsStore.newsSetByUser")
public List<NewsSet> getNewsSetsForUser(String userId) {
try {

Expand All @@ -317,6 +323,11 @@ public List<NewsSet> getNewsSetsForUser(String userId) {
}
}

@CacheEvict(cacheNames = {
"HibernateNewsStore.newsSetById",
"HibernateNewsStore.newsSetByUser",
"HibernateNewsStore.newsSetByUserAndName"
})
public void storeNewsSet(NewsSet set) {
try {

Expand All @@ -328,6 +339,7 @@ public void storeNewsSet(NewsSet set) {
}
}

@Cacheable("HibernateNewsStore.newsSetByUserAndName")
public NewsSet getNewsSet(String userId, String setName) {
try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void setNewsStore(NewsStore newsStore) {
/*
* Get the news set from the ID or search the dataabse for a suitable set or create a new
* set if one cannot be found.
*
* Initalise the NewsSet
*
* Initalise the NewsSet
*/
public NewsSet getNewsSet(String fname, PortletRequest request) {

Expand All @@ -64,11 +64,11 @@ public NewsSet getNewsSet(String fname, PortletRequest request) {
final String userId = userIdService.getUserId(request);

NewsSet set;

final Object mutex = PortletUtils.getSessionMutex(session);
synchronized (mutex) {
logger.debug("Got Mutex {} for userId={}", mutex, userId);

set = newsStore.getNewsSet(userId, fname);

if (set == null) {
Expand All @@ -77,8 +77,9 @@ public NewsSet getNewsSet(String fname, PortletRequest request) {
set.setUserId(userId);
set.setName(fname);
newsStore.storeNewsSet(set);
}

set = newsStore.getNewsSet(userId, fname); // get set_id
}

// Persistent set is now loaded but may still need re-initalising since last use.
// by adding setId to session, we signal that initialisation has taken place.
if (session.getAttribute("setId", PortletSession.PORTLET_SCOPE) == null) {
Expand Down
25 changes: 17 additions & 8 deletions src/main/resources/context/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,34 @@
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<context:component-scan base-package="org.jasig.portlet.newsreader">
<!-- Items in the following package are a part of the portlet (sub-)context,
not a part of the parent (main) context. -->
<context:exclude-filter type="regex" expression="org\.jasig\.portlet\.newsreader\.mvc\.portlet\..+"/>
</context:component-scan>
<context:annotation-config/>
<cache:annotation-driven />

<!-- EHCache Configuration -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true"/>

<bean id="newsCache"
<!-- Produces net.sf.ehcache.CacheManager -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true"/>

<!-- Wraps net.sf.ehcache.CacheManager in a class used by spring-cache -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="cacheManagerFactory" />

<bean id="newsCache"
class="org.springframework.cache.ehcache.EhCacheFactoryBean"
p:cacheManager-ref="cacheManager" p:cacheName="newsCache"/>
p:cacheManager-ref="cacheManagerFactory" p:cacheName="newsCache"/>

<util:list id="initializationServices">
</util:list>
Expand Down Expand Up @@ -149,12 +158,12 @@
<property name="elementsProvider" ref="elementsProvider"/>
<property name="filter">
<bean class="org.jasig.resourceserver.utils.cache.ConfigurablePageCachingFilter">
<constructor-arg ref="cacheManager"/>
<constructor-arg ref="cacheManagerFactory"/>
<constructor-arg value="org.jasig.portal.utils.cache.ConfigurablePageCachingFilter.PAGE_CACHE"/>
</bean>
</property>
</bean>

<bean id="CacheExpiresFilter" class="org.jasig.portlet.newsreader.mvc.AggregationAwareFilterBean">
<property name="elementsProvider" ref="elementsProvider"/>
<property name="filter">
Expand Down
19 changes: 16 additions & 3 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@
memoryStoreEvictionPolicy="LRU"/>

<cache name="org.jasig.portal.utils.cache.ConfigurablePageCachingFilter.PAGE_CACHE"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LRU" statistics="true" />

</ehcache>

<cache name="HibernateNewsStore.userRoles"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true" />
<cache name="HibernateNewsStore.newsSetById"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true"/>
<cache name="HibernateNewsStore.newsSetByUser"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true"/>
<cache name="HibernateNewsStore.newsSetByUserAndName"
eternal="false" maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true"/>

</ehcache>

0 comments on commit cdf6e74

Please sign in to comment.