栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis在项目中的应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis在项目中的应用

在aop模式下使用redis 的方式

(1)添加依赖


        
            
                org.springframework.boot
                spring-boot-dependencies
                2.3.2.RELEASE
                import
                pom
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

(2)配置文件中配置application.yml

spring:
  redis:
    host: 192.168.126.129  #写自己的ip
    port: 6379

(3)创建启动类,如果用注解方式需要在此处添加注解(@EnableCache)

package com.jt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@EnableCache
@SpringBootApplication
public class RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class,args);
    }
}

(4)获取菜单基于restemplate(方式一)

package com.jt.service;
import com.jt.dao.MenuMapper;
import com.jt.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;

@Service
public class MenuServiceImpl implements MenuService{
    @Autowired
    private MenuMapper menuMapper;

   // @Autowired
   // private RedisTemplate redisTemplate;

    @Resource(name="redisTemplate")
    private ValueOperations valueOperations;//从spring.io官方的data项目中去查这种注入方式
    
    @Override
    public Menu selectById(Long id) {
        //ValueOperations valueOperations = redisTemplate.opsForValue();
        Object obj=valueOperations.get(String.valueOf(id));
        if(obj!=null){
            System.out.println("Get Data from redis");
            return (Menu)obj;
        }
        Menu menu=menuMapper.selectById(id);
        valueOperations.set(String.valueOf(id), menu, Duration.ofSeconds(120));
        return menu;
    }
    @Override
    public Menu insertMenu(Menu menu) {
        menuMapper.insert(menu);
       // ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(String.valueOf(menu.getId()), menu, Duration.ofSeconds(120));
        return menu;
    }
    @Override
    public Menu updateMenu(Menu menu) {
        menuMapper.updateById(menu);
       // ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(String.valueOf(menu.getId()), menu, Duration.ofSeconds(120));
        return menu;
    }
}

(5)获取菜单基于注解(AOP方式,方式二)

package com.jt.service;

import com.jt.dao.MenuMapper;
import com.jt.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class DefaultMenuService implements MenuService{
    @Autowired
    private MenuMapper menuMapper;

    
    @Cacheable(value = "menuCache",key="#id")
    @Override
    public Menu selectById(Long id) {
        return menuMapper.selectById(id);
    }

    
    @CachePut(value = "menuCache",key="#menu.id")
    @Override
    public Menu insertMenu(Menu menu) {
         menuMapper.insert(menu);
         return menu;
    }

    @CachePut(value = "menuCache",key="#menu.id")
    @Override
    public Menu updateMenu(Menu menu) {
        menuMapper.updateById(menu);
        return menu;
    }
}

(6)设置redis中的序列化格式

package com.jt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;


@Configuration
public class CacheManagerConfig {
    
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //定义RedisCache配置
        RedisCacheConfiguration cacheConfig=
                RedisCacheConfiguration.defaultCacheConfig()
        //定义key的序列化方式
        .serializeKeysWith(
                RedisSerializationContext.
                   SerializationPair.fromSerializer(RedisSerializer.string()))
        //定义value的序列化方式
        .serializevaluesWith(
                RedisSerializationContext.SerializationPair
                .fromSerializer(RedisSerializer.json()));

        return  RedisCacheManager.builder(redisConnectionFactory)
               .cacheDefaults(cacheConfig)
               .build();//建造者模式(复杂对象的创建,建议使用这种方式,封装了对象的创建细节)
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号