springboot使用redis緩存

spring boot redis 使用 1

Redis:
Redis 是完全開源免費的,遵守BSD協(xié)議,是一個高性能的key-value數(shù)據(jù)庫。
Redis 與其他 key - value 緩存產(chǎn)品有以下三個特點:
Redis支持?jǐn)?shù)據(jù)的持久化,可以將內(nèi)存中的數(shù)據(jù)保存在磁盤中,重啟的時候可以再次加載進(jìn)行使用。
Redis不僅僅支持簡單的key-value類型的數(shù)據(jù),同時還提供list,set,zset,hash等數(shù)據(jù)結(jié)構(gòu)的存儲。
Redis支持?jǐn)?shù)據(jù)的備份,即master-slave模式的數(shù)據(jù)備份。

創(chuàng)新互聯(lián)專注于淮南網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供淮南營銷型網(wǎng)站建設(shè),淮南網(wǎng)站制作、淮南網(wǎng)頁設(shè)計、淮南網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造淮南網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供淮南網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

spring boot 中如何使用:

  1. 引入依賴 pom.xml

    <!-- redis -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. 配置redis

    #redis配置
    #Redis服務(wù)器地址
    spring.redis.host=192.168.5.10
    #Redis服務(wù)器連接端口
    spring.redis.port=6379
    #Redis數(shù)據(jù)庫索引(默認(rèn)為0)
    spring.redis.database=4
    #連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
    spring.redis.jedis.pool.max-active=50
    #連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
    spring.redis.jedis.pool.max-wait=3000
    #連接池中的最大空閑連接
    spring.redis.jedis.pool.max-idle=20
    #連接池中的最小空閑連接
    spring.redis.jedis.pool.min-idle=2
    #連接超時時間(毫秒)
    spring.redis.timeout=5000

  3. 修改啟動類 添加 @EnableCaching 注解

    @RestController@SpringBootApplication
    br/>@SpringBootApplication
    br/>@EnableCaching

  4. 實體類添加 序列化支持

    public class User implements Serializable

  5. 在 service 上加上緩存注解

    @Service
    public class MybatisUserService {
    
            @Autowired
            UserMapper userDao;
    
            public int add(User user){
                return   userDao.add(user);
            }
    
            public int update(User user){
                    return   userDao.update(user);
            }
    
            @Cacheable(value = "user-key")
            public User getById(long id){
                    System.out.println("從數(shù)據(jù)庫獲取數(shù)據(jù)");
                    return   userDao.findUserById(id);
            }
    }

----------------自動加入緩存結(jié)束------------
手動操作緩存:

@Component
public class RedisUtil {

        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        /**
         * 讀取緩存
         *
         * @param key
         * @return
         */
        public String get(final String key) {
                return redisTemplate.opsForValue().get(key);
        }

        /**
         * 寫入緩存
         */
        public boolean set(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().set(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 更新緩存
         */
        public boolean getAndSet(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().getAndSet(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 刪除緩存
         */
        public boolean delete(final String key) {
                boolean result = false;
                try {
                        redisTemplate.delete(key);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }
}

redis 簡單的操作介紹完成

分享題目:springboot使用redis緩存
網(wǎng)站路徑:http://muchs.cn/article20/gesejo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)網(wǎng)頁設(shè)計公司、網(wǎng)站營銷網(wǎng)站制作、網(wǎng)站收錄、全網(wǎng)營銷推廣

廣告

聲明:本網(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)

成都網(wǎng)站建設(shè)公司