java version of e-commerce spring cloud distributed micro-service b2b2c social e-commerce springboot integration Redis

Introduce dependencies:

E-Commerce Social Platform Source Plus Penguin Request: 103,877,4626.Add redis dependencies to the pom file:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configure Data Source

spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0

If your redis have a password, configure it.With these two steps, you can access the redis data.

Data Access Layer dao

Access redis through redisTemplate.

@Repository
public class RedisDao {

    @Autowired
    private StringRedisTemplate template;

    public  void setKey(String key,String value){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value,1, TimeUnit.MINUTES);//1 minute expiration
    }

    public String getValue(String key){
        ValueOperations<String, String> ops = this.template.opsForValue();
        return ops.get(key);
    }
}

unit testing

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {

    public static Logger logger= LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);
    @Test
    public void contextLoads() {
    }

    @Autowired
    RedisDao redisDao;
    @Test
    public void testRedis(){
        redisDao.setKey("name","forezp");
        redisDao.setKey("age","11");
        logger.info(redisDao.getValue("name"));
        logger.info(redisDao.getValue("age"));
    }
}

Start the unit test and you find that the console prints:

forezp

11

Unit test passed;

B2B2C E-Commerce Platform Source Required by JAVASpring Cloud Large Enterprise Distributed Micro-Service Cloud: 10387474626

Tags: Programming Redis Spring Database

Posted on Thu, 07 Nov 2019 07:41:46 -0800 by aneuryzma