srping 集成 redis
pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>redis</groupId> <artifactId>redis</artifactId> <version>0.0.1-SNAPSHOT</version> <build> </build> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> </dependencies> </project>
spring 配置文件代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--注解说明 --> <context:annotation-config /> <!-- 把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="com.test.**" /> <!-- redis工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="192.168.0.124" p:port="6379" p:password="123456" /> <!-- redis服务封装 --> <bean id="redisService" class="com.test.redis.RedisService"> </bean>
或者这样将spring和redis集成
spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties" /> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxActive" value="${redis.maxActive}" /> <property name="maxWait" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="userDao" class="com.x.dao.impl.UserDao" /> </beans>
redis.properties:
# Redis settings redis.host=localhost redis.port=6379 redis.pass=java2000_wl redis.maxIdle=300 redis.maxActive=600 redis.maxWait=1000 redis.testOnBorrow=true
redisService:
package com.test.redis;import java.util.Set;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import redis.clients.jedis.Jedis;publicclassRedisService{/**
* 通过key删除(字节)
* @param key
*/publicvoiddel(byte[] key){this.getJedis().del(key);}/**
* 通过key删除
* @param key
*/publicvoiddel(String key){this.getJedis().del(key);}/**
* 添加key value 并且设置存活时间(byte)
* @param key
* @param value
* @param liveTime
*/publicvoidset(byte[] key,byte[] value,int liveTime){this.set(key, value);this.getJedis().expire(key, liveTime);}/**
* 添加key value 并且设置存活时间
* @param key
* @param value
* @param liveTime
*/publicvoidset(String key,String value,int liveTime){this.set(key, value);this.getJedis().expire(key, liveTime);}/**
* 添加key value
* @param key
* @param value
*/publicvoidset(String key,String value){this.getJedis().set(key, value);}/**添加key value (字节)(序列化)
* @param key
* @param value
*/publicvoidset(byte[] key,byte[] value){this.getJedis().set(key, value);}/**
* 获取redis value (String)
* @param key
* @return
*/publicStringget(String key){String value =this.getJedis().get(key);return value;}/**
* 获取redis value (byte [] )(反序列化)
* @param key
* @return
*/publicbyte[]get(byte[] key){returnthis.getJedis().get(key);}/**
* 通过正则匹配keys
* @param pattern
* @return
*/publicSet<String> keys(String pattern){returnthis.getJedis().keys(pattern);}/**
* 检查key是否已经存在
* @param key
* @return
*/publicboolean exists(String key){returnthis.getJedis().exists(key);}/**
* 清空redis 所有数据
* @return
*/publicString flushDB(){returnthis.getJedis().flushDB();}/**
* 查看redis里有多少数据
*/publiclong dbSize(){returnthis.getJedis().dbSize();}/**
* 检查是否连接成功
* @return
*/publicString ping(){returnthis.getJedis().ping();}/**
* 获取一个jedis 客户端
* @return
*/privateJedis getJedis(){if(jedis ==null){return jedisConnectionFactory.getShardInfo().createResource();}return jedis;}privateRedisService(){}//操作redis客户端privatestaticJedis jedis;@Autowired@Qualifier("jedisConnectionFactory")privateJedisConnectionFactory jedisConnec
tionFactory;}
public static void main(String[] args) throws InterruptedException { ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); //这里已经配置好,属于一个redis的服务接口 RedisService redisService = (RedisService) app.getBean("redisService"); String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG System.out.println(ping); //首先,我们看下redis服务里是否有数据 long dbSizeStart = redisService.dbSize(); System.out.println(dbSizeStart); redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟) String username = redisService.get("username");//取值 System.out.println(username); redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位) String username1 = redisService.get("username1"); System.out.println(username1); Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间 String liveUsername1 = redisService.get("username1"); System.out.println(liveUsername1);//输出null //是否存在 boolean exist = redisService.exists("username"); System.out.println(exist); //查看keys Set<String> keys = redisService.keys("*");//这里查看所有的keys System.out.println(keys);//只有username username1(已经清空了) //删除 redisService.set("username2", "oyhk2"); String username2 = redisService.get("username2"); System.out.println(username2); redisService.del("username2"); String username2_2 = redisService.get("username2"); System.out.println(username2_2);//如果为null,那么就是删除数据了 //dbsize long dbSizeEnd = redisService.dbSize(); System.out.println(dbSizeEnd); //清空reids所有数据 //redisService.flushDB(); }
相关推荐
redis缓存安装软件和学习开发相关文档,包括redis持久化方式的教案,绝对满满的干货
spring boot —— redis 缓存注解使用教程 示例项目地址: 依赖 在pom文件添加如下依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis 配置 在application.yml配置文件添加...
2. 添加键值的缓存生命周期的设置 3. 添加相关帮助菜单以及反馈信息 4. 优化list,set,hash,zset的分页操作,很大程度上避免了程序的崩溃 5. 优化布局,使用更方便快捷 6. 修复若干bugs 2015-07-04 初版发布1.0 1. ...
问题初现 某基于node.js开发的业务系统向外提供了一个dubbo服务,提供向第三方缓存查询、设置多项业务数据并聚合操作结果。在QPS达到800时(两台虚拟机,每台机器4Core8G4node进程),在监控平台上出现了非常多的...
十三年初于CSDN上传了一份自己制作的达人课课程,感兴趣的朋友可以看一下:项目简介ssm系列 :Spring + SpringMVC + Mybatis + easyUI整合 perfect-ssm:RESTful API + redis缓存 :前扩展分离+部署部署(springboot...
本视频《SpringBoot高级》属于下部,着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),...
源码、课件 1、尚硅谷-SpringBoot高级-缓存-JSR107简介 2、尚硅谷-SpringBoot高级-缓存-Spring缓存抽象简介 3、尚硅谷-SpringBoot高级-缓存-基本环境搭建 4、尚硅谷-SpringBoot高级-缓存-@Cacheable初体验 ...
系统总览有6种服务: Redis缓存(ElastiCache)(pocketwatch-cache) GCP数据存储区(gstore) 计时员(运行cron作业并管理消息延迟的EB SQS工作者)(怀表机制) 主管(负责计时的EB SQS工作人员,确保所有正在...
数据库和缓存:SQL优化、索引、Redis、Memcached等 分布式系统:负载均衡、集群、分布式事务、分布式锁等 使用人群: Java初/中级工程师,准备面试、,巩固和提升Java技能 -高级工程师和架构师,掌握核心原理,解决复杂问题...
:RESTful API + redis缓存 :前扩展分离+部署部署(springboot + vue) :dubbo服务化 :springcloud技术栈实现微服务化 特别提示:数据库sql脚本文件在resources / db目录下! 本项目为ssm系列的第一篇, ...
前言使用yii2.0 + jquery + bootstrap构造的简易博客系统,纯属为了学习(2016年初) QQ: 292304400 ,gmail: 主要功能前后台登录文章模块,点赞,评论,回复,收藏,分享朋友圈空间等留言板用户管理邮件发送redis...
基于缓存(Redis等)实现分布式锁; 基于Zookeeper实现分布式锁; 基于注册中心consul 实现分布式锁; 分布式场景中的数据一致性问题一直是一个比较重要的话题。分布式的CAP理论告诉我们“任何一个分布式系统都无法...
蒸汽遥控 消耗Steam api的超级秘密项目 目前支持 通过 steamid 搜索显示基本的个人资料信息 简要路线图 Redis缓存 在某些游戏中显示玩家的背包物品
CmlPHP支持分布式数据库、分布式缓存(使用memcache缓存驱动),支持文件、memcache、redis、apc等缓存,支持多种url模式,支持URL路由,支持多项目集成、支持第三方扩展、支持插件... 好文本网站内容管理系统(GCMS...
内存缓存会话管理器 memcached-session-manager是一个tomcat会话管理器,用于将会话保持在memcached或Redis中,以实现高可用性,可伸缩性和容错性的Web应用程序。 它支持粘性和非粘性配置,并且当前与tomcat 6.x,7...
缓存策略:在合适的地方引入缓存机制(如Redis、Memcached)提高系统性能。 分页与批量处理:对于大数据量的处理,采用分页或流式处理,减轻服务器压力。 服务化架构:采用微服务架构,将复杂系统拆分为多个独立可...
去服务器gonet游戏服务器架构,mmo架构,分散的snowflake64为整形uuid,ai行为树,配置数据,游戏大部分都在内存处理,分布式缓存redis,增加了db模块读取blob数据。设计之初,建立在actor模式下的; rpc,以及消息...
CmlPHP支持分布式数据库、分布式缓存(使用memcache缓存驱动),支持文件、memcache、redis、apc等缓存,支持多种url模式,支持URL路由,支持多项目集成、支持第三方扩展、支持插件... 特点: 01轻量级网站内容...
在将Redis缓存作为开发环境的容器运行时,定义了类似的情况。 但是,在实际的生产环境中,建议您将数据库(在本例中为SQL Server和Redis)设置为HA(高可用性)服务,如Azure SQL数据库,Redis服务或任何其他集群...
缓存、代理、队列设置克隆这个 repo,运行npm install 。 安装 redis 并在 localhost:6379 上运行来自 。 这可以在具有端口转发的 6379 上具有开放端口的 VM 上完成: wget ...