使用Redis進行簡單的資料快取

2022-07-06 15:00:13 字數 2607 閱讀 7343

引入spring-data-redis包、jedis、connection-pool包

<

bean

id="redisconnection"

class

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

>

<

property

name

="hostname"

value

="localhost"

>

property

>

<

property

name

="port"

value

="6379"

>

property

>

bean

>

<

bean

id="redistemplate"

class

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

>

<

property

name

="connectionfactory"

ref="redisconnection"

>

property

>

bean

>

做乙個簡單的測試

@test

public

void

test1()

jd.close();

}@test

public

void

test2()

j.close();

}

自己寫乙個工具類將,進行序列化與反序列化,

public

class

serializableutil

catch

(ioexception e)

finally

catch

(ioexception e)

try

catch

(ioexception e) }}

public

static object bytetoobject(byte

bytes)

catch

(exception e)

finally

catch

(ioexception e)

try

catch

(ioexception e) }}

view code

測試:

@test

public

void

test4()

@test

public

void

test5()

上面只是簡單的使用自己寫的乙個工具類進行序列化與反序列化,實際開發中還是使用工具進行的,

寫乙個控制器,對其進行單元測試

@runwith(springjunit4classrunner.class

)//這裡的意思是進行乙個spring環境的配置

public

class

testredis_data

}

然後在對自己有快取需求的方法進行開啟redis快取,

//

@controller

@restcontroller //

相當於@controller 和 @responsebody 相結合的功能

public

class

deptcontroller

return

deptlist;

}//查詢

public deptbean selelctdeptbyid(@requestparam("no") int

id)

system.out.println("從快取中取的資料");

return

bean;

}//分頁查詢

//刪除

public

int deletedeptbyid(int

deptno)

}

當然如果你需要在專案啟動時候就載入到記憶體中,則可以這樣,另寫乙個專門載入需要快取的資料

@component

public

class

initloaddata

}

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

1編寫redis類,主要是對資料的讀寫操作 public class redisutil 批量刪除key param pattern public void removepattern final string pattern 刪除對應的value param key public void rem...

使用Redis進行限流

通過使用註解 提供介面形式與引數形式2種方式 針對性的對介面進行限流,底層使用redis配合lua指令碼實現令牌桶。public class redisratelimiter public boolean tryacquire string flag,int maxpermits,int addra...

使用redis進行快取

enablecaching 開啟快取 configuration 配置類 public class redisconfig extends cachingconfigurersupport bean public cachemanager cachemanager redisconnectionfa...