ehcache springmvc 整合

ehcache-local.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">

	<diskStore path="../temp/springbaseEhCache" />

	<!-- 默認緩存配置. 自動失效:最後一次訪問時間間隔300秒失效,若沒有訪問過自建立時間600秒失效。-->
	<defaultCache maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
		overflowToDisk="true" statistics="true"/>
	
	<!-- 系統緩存 -->
	<cache name="sysCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>
	
	<!-- 用戶緩存 -->
	<cache name="userCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>
	
	<!-- 集團緩存 -->
	<cache name="corpCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>
	
	<!-- 內容管理模塊緩存 -->
	<cache name="cmsCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>
    
	<!-- 工做流模塊緩存 -->
	<cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true" statistics="true"/>
	
    <!-- 簡單頁面緩存 -->
    <cache name="pageCachingFilter" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="120"
    	timeToLiveSeconds="120" overflowToDisk="true" memoryStoreEvictionPolicy="LFU" statistics="true"/>
	
	<!-- 系統活動會話緩存 -->
    <cache name="activeSessionsCache" maxEntriesLocalHeap="10000" eternal="true" overflowToDisk="true"
           diskPersistent="true" diskExpiryThreadIntervalSeconds="600" statistics="true"/>
    	
</ehcache>

spring-context.xml

<!-- 緩存配置 -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:cache/ehcache-local.xml" />
	</bean>

Controller

package com.tiger;



import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;


@Controller
@RequestMapping(value="ehcache")
public class EhCache {
	@Autowired
	private CacheManager cacheManager;
	
	
	private String cacheName="activeSessionsCache";
	
	
	
	@RequestMapping(value="index")
	public   String   test(){
		return "views/ehcache";
	}
	
	@ResponseBody
	@RequestMapping(value="add",produces="text/html;charset=UTF-8")
	public   Object   addEache(HttpServletRequest request){
		Cache cache = cacheManager.getCache(cacheName);
		
		String key = request.getParameter("key");
		String value = request.getParameter("value");
		System.out.println("key="+key+"--value="+value);
		Element element = new Element(key, value);
		cache.put(element);
		return "添加成功!";
	}
	
	@ResponseBody
	@RequestMapping(value="show" ,produces="text/html;charset=UTF-8")
	public   Object   show(HttpServletRequest request){
		Cache cache = cacheManager.getCache(cacheName);
		String key = request.getParameter("key");
		Element element = cache.get(key);
		if(element!=null)
		{
			Object objectValue = element.getObjectValue();
			System.out.println(objectValue);
			return objectValue;
		}
		return "空";
	}
	@ResponseBody
	@RequestMapping(value="del",produces="text/html;charset=UTF-8")
	public   Object   delete(HttpServletRequest request){
		Cache cache = cacheManager.getCache(cacheName);
		String key = request.getParameter("key");
		cache.remove(key);
		return "已經刪除";
	}

}

jsp/

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>



<script type="text/javascript" src="/springbase/static/jquery/jquery-1.8.3.min.js"></script>

<script type="text/javascript">

	function add(){
	    $.ajax({
            url :"/springbase/ehcache/add",
            type: 'post',
            dataType : "text",
            data : {key:$('#key').val(),value:$('#value').val()},
            async : false, // 同步請求
            success : function(data) {
            	  $('#showResult').text(data)
            }
        });
	}
	function del(){
	    $.ajax({
            url :"/springbase/ehcache/del",
            type: 'post',
            dataType : "text",
            data : {key:$('#key').val()},
            async : false, // 同步請求
            success : function(data) {
            	  $('#showResult').text(data)
            }
        });
	}
	function show(){
	    $.ajax({
            url :"/springbase/ehcache/show",
            type: 'post',
            dataType : "text",
            data : {key:$('#key').val()},
            async : false, // 同步請求
            success : function(data) {
            	  $('#showResult').text(data)
            }
        });
	}

</script>

</head>

<body>
<a>Hello World!---jsp/index--1</a>

<h3>cc--${name}</h3>


key=  <input  type="text"   id="key"  name="key"    value="name"> <br>
value=<input  type="text" id="value"  name="value"  value="張三"> <br>

<a  id="showResult"></a>
<br>

<input  type="button"  onclick="add();"  value="添加" >  <br>

<input  type="button"  onclick="del();"  value="del3">

<input  type="button"  onclick="show();"  value="show">

</body>
</html>
相關文章
相關標籤/搜索