redistemplate文档用法_Spring boot集成Redis(2)—RedisTemplate的使用来存储Map集合

前言:上一篇文章我们用的是StringRedisTemplate,但是它存在一点问题,也迫使我重新写了代码,问题是:在我们往缓存中存入数字形式的String类型时,我们在利用Spring could将获取到的数据发送到另一服务时,我们发现数据已经被强转为Integer类型了,因为我们可能传输的数据庞大,类型多样,为了统一类型,以及开发方便,所以我将缓存改成RedisTemplate这种类型,进行增删改查的操作,文中没有特别举例更新操作,其更新操作与添加操作一样,当key一样时进行添加就会覆盖原value值,完成更新。RedisTemplate需要我们自己去配置它并进行实例化。接下来,举例子,上代码:

首先建立Spring boot项目添加Redis依赖

下载导入IDE,我们观察pom.xml文件:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.test

redis

0.0.1-SNAPSHOT

jar

redis

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.1.0.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

org.springframework.boot

spring-boot-maven-plugin

1.配置application.properties

#redis

spring.redis.host=主机地址

spring.redis.password=admin

spring.redis.port=6379

spring.redis.timeout=10000

spring.redis.jedis.pool.max-idle=200

spring.redis.jedis.pool.min-idle=300000

spring.redis.jedis.pool.max-active=400

spring.redis.jedis.pool.max-wait=10000

2.我们写配置配置类实例化RedisTemplate

package com.test.redis.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

import org.springframework.data.redis.connection.RedisConnectionFactory;

@Configuration

public class RedisConfig {

/**

* 实例化 RedisTemplate 对象

*

* @return

*/

@Bean

public RedisTemplate functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate redisTemplate = new RedisTemplate<>();

initDomainRedisTemplate(redisTemplate, redisConnectionFactory);

return redisTemplate;

}

/**

* 设置数据存入 redis 的序列化方式,并开启事务

*

* @param redisTemplate

* @param factory

*/

private void initDomainRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) {

// 如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to

// String!

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

// 开启事务

redisTemplate.setEnableTransactionSupport(true);

redisTemplate.setConnectionFactory(factory);

}

}

3.写缓存操作的Service层,进行增删改查方法的定义:

package cn.com.dhcc.idatabus.admin.console.service;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.redis.core.HashOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import org.springframework.stereotype.Service;

@Service

public class RedisService {

@Resource

private RedisTemplate template;

/**

* 存储数据或修改数据

*

* @param modelMap

* @param mapName

*/

public void setKey(String mapName, Map modelMap) {

HashOperations hps = template.opsForHash();

hps.putAll(mapName, modelMap);

}

/**

* 获取数据Map

*

* @param mapName

* @return

*/

public Map getMapValue(String mapName) {

HashOperations hps = this.template.opsForHash();

return hps.entries(mapName);

}

/**

* 获取数据value

*

* @param mapName

* @param hashKey

* @return

*/

public Object getValue(String mapName, String hashKey) {

HashOperations hps = this.template.opsForHash();

return hps.get(mapName, hashKey);

}

/**

* 批量删除缓存数据

*

* @param keys

*/

public void deleteData(List keys) {

// 执行批量删除操作时先序列化template

template.setKeySerializer(new JdkSerializationRedisSerializer());

template.delete(keys);

}

}

4.本次例子的实体类

package com.test.redis.entity;

public class User {

private Integer id;

private String name;

private String password;

public User() {

super();

}

public User(Integer id, String name, String password) {

super();

this.id = id;

this.name = name;

this.password = password;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String toString() {

return "User [id=" + id + ", name=" + name + ", password=" + password + "]";

}

}

5.编写Controller层,来实现缓存的操作

package com.test.redis.web;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.test.redis.entity.User;

import com.test.redis.service.RedisService;

@Controller

public class UserController {

private static final String mapName="mapName";

@Autowired

private RedisService redisService;

@GetMapping( "/templateAdd.do")

@ResponseBody

public Map addUser(HttpServletRequest request){

Map modelMap=new HashMap();

User user=new User();

user.setName("hehename");

user.setPassword("hehePassword");

//存放hash值

modelMap.put("name", user.getName());

modelMap.put("password", user.getPassword());

redisService.setKey(mapName, modelMap);

//获取map集合

Map modelMap1= redisService.getMapValue(mapName);

Object value= redisService.getValue(mapName, "name");

System.out.println(" value : "+value);

modelMap1.put("从缓存中根据key取到的value", value);

return modelMap1;

}

@GetMapping( "/templateDelete.do")

@ResponseBody

public Map deleteUser(HttpServletRequest request){

//获取即将删除的key值,这里我们做的批量删除

List keys=new ArrayList<>();

keys.add("heheanme");

//开始执行删除操作

redisService.deleteData(keys);

//获取map集合

Map modelMap1= redisService.getMapValue(mapName);

Object value= redisService.getValue(mapName, "name");

System.out.println(" value : "+value);

modelMap1.put("从缓存中根据key取到的value", value);

return modelMap1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值