redis整合spring實現對資料的快取

2021-08-03 23:41:14 字數 4607 閱讀 3836

1編寫redis類,主要是對資料的讀寫操作

public class redisutil 

}/**

* 批量刪除key**

@param

pattern

*/public void

removepattern(final string pattern)

/*** 刪除對應的value**

@param

key*/

public void

remove(final string key)

}/**

* 判斷快取中是否有對應的value**

@param

key*

@return

*/public boolean

exists(final string key)

/*** 讀取快取**

@param

key*

@return

*/public object get(final string key)

/*** 寫入快取**

@param

key*

@param

value

*@return

*/public boolean

set(final string key, object value) catch (exception e)

return result;

} /**

* 寫入快取**

@param

key*

@param

value

*@return

*/public boolean

set(final string key, object value, long expiretime) catch (exception e)

return result;

} public void

setredistemplate(

redistemplate, object> redistemplate)

}

2***,用於在讀取資料前訪問,如果有結果,則不繼續下去

public class methodcacheinterceptor implements methodinterceptor 

// 寫入快取

value = invocation.proceed();

if (value != null)

}).start();

} } catch (exception e)

}return value;

} /**

* 建立快取key**

@param

targetname

*@param

methodname

*@param

arguments

*/private string getcachekey(string targetname, string methodname,

object arguments)

}return sbu.tostring();

} public void

setredisutil(redisutil redisutil)

}

3spring配置:

id="poolconfig"

class

="redis.clients.jedis.jedispoolconfig"

>

name

="maxidle"

value

="100"

/>

name

="maxwaitmillis"

value

="1000"

/>

name

="testonborrow"

value

="true"

/>

id="testdao"

class

="com.smart.domain.testdao"

/>

id="testservice"

class

="com.smart.domain.testservice"

p:testdao-ref

="testdao"

/>

id="connectionfactory"

class

="org.springframework.data.redis.connection.jedis.jedisconnectionfactory"

>

name

="poolconfig"

ref="poolconfig"

/>

name

="port"

value

="6379"

/>

name

="hostname"

value

="127.0.0.1"

/>

name

="timeout"

value

="100000"

>

id="redistemplate"

class

="org.springframework.data.redis.core.redistemplate"

>

name

="connectionfactory"

ref="connectionfactory"

/>

name

="keyserializer"

>

class

="org.springframework.data.redis.serializer.stringredisserializer"

/>

name

="valueserializer"

>

class

="org.springframework.data.redis.serializer.jdkserializationredisserializer"

/>

id="methodcacheinterceptor"

class

="com.smart.domain.methodcacheinterceptor"

>

name

="redisutil"

ref="redisutil"

/>

id="redisutil"

class

="com.smart.domain.redisutil"

>

name

="redistemplate"

ref="redistemplate"

/>

<

aop:config>

<

aop:pointcut

id="loginpoint"

expression

="execution(* getservice(..))"

/>

<

aop:advisor

pointcut-ref

="loginpoint"

advice-ref

="methodcacheinterceptor"

/>

aop:config>

4配置的幾個測試

public class testdao 

}

public class testservice 

public string getservice()

}

5測試:

@runwith(springjunit4classrunner.class)

public class redistest

}

執行結果:

com.smart.domain.testservice_getservice

com.smart.domain.testservice_getservice

result success,result success

因為快取時間比較長,我這個例子不是第一次跑了,所以dao中的sql依據也沒有執行

spring整合redis集群配置

pom.xml org.springframework.data spring data redis 1.7.4.release org.slf4j slf4j api 這裡為什麼要排除slf日誌?因為我的框架使用的是log4j,如果有slf,那麼log4j的日誌將不生效。所以要排除。因為sprin...

Spring整合redis方法總結

目的 總結spring總結redis的幾種方法 一 與spring整合成為template 1 properties檔案 redis redis.host 192.168.redis.port 6379 redis.password redis.maxidle 100 redis.maxtotal ...

Spring和Redis整合理解

remote dictionary server redis 是乙個由salvatore sanfilippo寫的key value儲存系統。redis是乙個開源的使用ansi c語言編寫 遵守bsd協議 支援網路 可基於記憶體亦可持久化的日誌型 key value資料庫,並提供多種語言的api。它...