本篇內(nèi)容主要講解“Springboot怎么整合redis實現(xiàn)簡單的數(shù)據(jù)寫入和讀取”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Springboot怎么整合redis實現(xiàn)簡單的數(shù)據(jù)寫入和讀取”吧!
創(chuàng)新互聯(lián)是專業(yè)的哈巴河網(wǎng)站建設(shè)公司,哈巴河接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行哈巴河網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
引入maven依賴: <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redission}</version> </dependency> redisUtil.java
package com.gllic.workweixin.utils; import org.redisson.api.RBucket; import org.redisson.api.RedissonClient; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; @Component public class RedisUtil { @Resource private RedissonClient redissonClient; public boolean setString(String key, Object value, long time) { try { RBucket rBucket = redissonClient.getBucket(key); if (time > 0) { rBucket.set(value,time,TimeUnit.SECONDS); } else { rBucket.set(value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } public String getString(String key) { if(key==null) return null; RBucket rBucket=redissonClient.getBucket(key); Object o = rBucket.get(); return o == null ? null : o.toString(); } // public boolean setString(String key, Object value, long time) { // try { // if (time > 0) { // redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); // } else { // redisTemplate.opsForValue().set(key, value); // } // return true; // } catch (Exception e) { // e.printStackTrace(); // return false; // } // } // // public String getString(String key) { // if(key==null) // return null; // Object o = redisTemplate.opsForValue().get(key); // return o == null ? null : o.toString(); // } } 寫入: RedisUtil.setString("key","value",time); 讀取: RedisUtil.getString("key");
到此,相信大家對“Springboot怎么整合redis實現(xiàn)簡單的數(shù)據(jù)寫入和讀取”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站標(biāo)題:Springboot怎么整合redis實現(xiàn)簡單的數(shù)據(jù)寫入和讀取
網(wǎng)頁URL:http://sd-ha.com/article2/ghdhoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站收錄、動態(tài)網(wǎng)站、用戶體驗、網(wǎng)頁設(shè)計公司、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)