Skip to content

Instantly share code, notes, and snippets.

@schnell18
Created May 24, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schnell18/4b75d2480e3e04e309f5 to your computer and use it in GitHub Desktop.
Save schnell18/4b75d2480e3e04e309f5 to your computer and use it in GitHub Desktop.
Spring cache(ehcache and guava cache) bootstrap
mkdir -p src/main/{java,resources}/org/home/springCache/{guava,ehcache}
cat <<'EOF' > .gitignore
.swp
.swo
.bak
build/
.gradle/
.vagrant/
EOF
cat <<'EOF' > build.gradle
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-context:4.1.5.RELEASE'
compile 'org.springframework:spring-context-support:4.1.5.RELEASE'
compile 'com.google.guava:guava:18.0'
compile 'net.sf.ehcache:ehcache:2.10.0'
testCompile 'junit:junit:4.11'
}
/* vim: set ai nobk nu expandtab sw=4 ts=4 tw=72 syntax=groovy : */
EOF
cat <<'EOF' > src/main/java/org/home/springCache/App.java
package org.home.springCache;
public class App {
public static void main(String[] args) {
}
}
/* vim: set ai nobk nu expandtab sw=4 ts=4 tw=72 syntax=java : */
EOF
cat <<'EOF' > src/main/java/org/home/springCache/ehcache/CachingConfig.java
package org.home.springCache.ehcache;
import net.sf.ehcache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableCaching
public class CachingConfig {
@Bean
public EhCacheCacheManager cacheManager(CacheManager cm) {
return new EhCacheCacheManager(cm);
}
@Bean
public EhCacheManagerFactoryBean ehcache() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean =
new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(
new ClassPathResource("org/home/springCache/ehcache/ehcache.xml")
);
return ehCacheManagerFactoryBean;
}
}
/* vim: set ai nobk nu expandtab sw=4 ts=4 tw=72 syntax=java : */
EOF
cat <<'EOF' > src/main/resources/org/home/springCache/ehcache/ehcache.xml
<ehcache>
<cache name="yourCache"
maxBytesLocalHeap="50m"
timeToLiveSeconds="100">
</cache>
</ehcache>
EOF
cat <<'EOF' > src/main/java/org/home/springCache/guava/CachingConfig.java
package org.home.springCache.guava;
import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CachingConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
GuavaCache cache1 = new GuavaCache(
"yourCache1",
CacheBuilder.newBuilder().build()
);
GuavaCache cache2 = new GuavaCache(
"yourCache2",
CacheBuilder
.newBuilder()
.expireAfterAccess(30, TimeUnit.MINUTES)
.build()
);
simpleCacheManager.setCaches(Arrays.asList(cache1, cache2));
return simpleCacheManager;
}
}
/* vim: set ai nobk nu expandtab sw=4 ts=4 tw=72 syntax=java : */
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment