spring 的redis操作类RedisTemplate

spring 集成的redis操作几乎都在RedisTemplate内了。

已spring boot为例,

再properties属性文件内配置好

redis的参数

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=redispass
spring.redis.database=0
spring.redis.timeout=5000

  再到 Application启动类下加入以下代码:

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
    {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory); //线程安全的连接工程
        template.setKeySerializer(jackson2JsonRedisSerializer); //key序列化方式采用fastJson
        template.setValueSerializer(jackson2JsonRedisSerializer); //value序列化方式
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

  这样就可以在需要的时候直接使用自动注入(@Autowired)获取redisTemplate操作redis了:

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public Result selectUserById(String id) {
		if(StringUtils.isEmpty(id)){
			throw new BusinessException(CommonConstants.ErrorCode.ERROR_ILLEGAL_PARAMTER);//ID为空
		}
		String redisCache = redisTemplate.opsForValue().get(CacheKeys.SELECT_USER_PHONE_KEYS+id);
		if(redisCache!=null){
			Result result = new Gson().fromJson(redisCache, Result.class);
			if(result.getResult() == null){
				throw new BusinessException(CommonConstants.ErrorCode.ERROR_ILLEGAL_USER);//用户不存在
			}
			return result;
		}
		User selectByPrimaryKey = userMapper.selectByPrimaryKey(id); //自己项目的Dao层
		redisTemplate.opsForValue().set(CacheKeys.SELECT_USER_PHONE_KEYS+id, CommonConstants.GSONIGNORENULL.toJson(new Result(selectByPrimaryKey)), 1, TimeUnit.HOURS); //缓存有效时间为1天
		if(selectByPrimaryKey == null){
			throw new BusinessException(CommonConstants.ErrorCode.ERROR_ILLEGAL_USER);//用户不存在
		}
		return new Result(selectByPrimaryKey);
	}

  

				
时间: 2024-08-21 22:08:26

spring 的redis操作类RedisTemplate的相关文章

使用Spring Data Redis操作Redis(二)

上一篇讲述了Spring Date Redis操作Redis的大部分主题,本篇介绍Redis的订阅和发布功能在Spring应用中的使用. 1. Redis的Pub/Sub命令 Redis的订阅和发布服务有如下图6个命令,下面分别对每个命令做简单说明. publish: 向指定的channel(频道)发送message(消息) subscribe:订阅指定channel,可以一次订阅多个 psubscribe:订阅指定pattern(模式,具有频道名的模式匹配)的频道 unsubscribe:取消

使用Spring Data Redis操作Redis(集群版)

继上一篇文章http://www.cnblogs.com/EasonJim/p/7804545.html使用Spring Data Redis操作Redis用的是单机版,如果是集群版的集成其实差别不大.主要思路如下: 1.先建立连接工厂,这个连接工厂是用来设置IP,端口,账号密码等.(在这一步时,传递一个集群的地址列表,不再是单独一个去指定) 2.通过连接工厂建立Session. 3.然后在代码上注入Session进行使用. 简要实现步骤,操作的方法和单机版的类似: 一.使用spring-dat

设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类

1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个数的控制并节约系统资源.假设希望在系统中某个类的对象仅仅能存在一个.单例模式是最好的解决方式. 2 模式核心思想 1)某个类仅仅能有一个实例: 2)它必须自行创建这个实例: 3)它必须自行向整个系统提供这个实例. 3 模式架构图 4 项目应用 4.1 需求说明 CleverCode在实际的PHP项目

php的redis 操作类,适用于单台或多台、多组redis服务器操作

redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台.多组redis服务器操作 使用方法: 1.$rs=new mz_redis();$rs->load_config_file('redis_config1.php');$www=$rs->connect(1,true,0)==单台读连接,连接read_array第一个元素对应的redis服务器中的随

使用Spring Data Redis操作Redis(一)

Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring Framework对JDBC支持一样. 项目主页:http://projects.spring.io/spring-data-redis/ 项目文档:http://docs.spring.io/spring-data/redis/docs/1.5.0.RELEASE/reference/html/ 本文主要介绍Spring Data Redis的实际使用. 1

Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)

1.添加依赖架包: 1 <dependency> 2 <groupId>org.springframework.data</groupId> 3 <artifactId>spring-data-redis</artifactId> 4 <version>${spring-data-redis.version}</version> 5 </dependency> 6 <!-- 用于jedis-spring-

使用Spring Data Redis操作Redis(单机版)

Jedis是一款Java连接Redis的客户端,Spring基于Jedis进行了封装,提供了简洁的操作Redis的方法.那就是Spring Data Redis.其实后来研究发现,Spring Data Redis集成不止Jedits这一款,还有很多款,这些都可以通过注入连接工厂来去指定. 要使用Spring Data Redis需要做如下步骤的操作思路: 1.先建立连接工厂,这个连接工厂是用来设置IP,端口,账号密码等. 2.通过连接工厂建立Session. 3.然后在代码上注入Session

Java Spring 与 Redis 操作封装源码

Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用于持久性. 相比许多键值数据存储,Redis拥有一套较为丰富的数据类型. Redis可以将数据复制到任意数量的从服务器. Redis 优势如下: 异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录. 支持丰富的数据类型:Redis支持最大多数开发人员已经知道像列表,集

用C#封装的ServiceStack.redis操作类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServiceStack.Redis; namespace TestRedis { class RedisHelper:IDisposable { /*[email protected] All Rights Reserved * Author:Mars