java web项目配置Redis(windows环境)
来源:互联网 发布:seo外包服务公司 编辑:程序博客网 时间:2024/05/16 23:01
背景介绍
- redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。
- 在此介绍一下怎么将redis配置到Java Web项目中。
安装redis
- 环境准备:win 7环境、reids3.0
- 步骤1:下载redis解压到任意盘符,结果如下:
- 点击redis-server.exe启动redis服务,出现如下界面:
- 选择redis-cli.exe启动redis客户端,进行测试。输入set mykey redis 回车,输入 get mykey 回车,输出:“redis”。安装完成
项目配置reids
spring项目下,需要导入commons-pool-1.5.6.jar、commons-pool2-2.4.2.jar、jedis-2.7.3.jar、spring-data-redis-1.6.0.RELEASE.jar文件。在spring配置文件中加入以下元素:
<bean id="redisUtil" class="com.project.controller.Redis.RedisUtil"> <!-- 初始化类 --> <property name="addr"><value>127.0.0.1</value></property> <!-- 访问地址,默认本地 --> <property name="port"><value>6379</value></property> <!-- 端口号 --> <property name="auth"><value>master</value></property> <property name="maxIdle"><value>200</value></property> <property name="maxActive"><value>1024</value></property> <property name="maxWait"><value>10000</value></property> <property name="timeOut"><value>10000</value></property> <property name="testOnBorrow"><value>true</value></property> </bean>
RedisUtil代码:
public class RedisUtil implements Serializable{ private static final long serialVersionUID = -1149678082569464779L; //Redis服务器IP private static String addr; //Redis的端口号 private static int port; //访问密码 private static String auth; //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int maxActive; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 private static int maxIdle; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; private static int maxWait; private static int timeOut; //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static boolean testOnBorrow; public static Jedis jedis;//非切片额客户端连接 public static JedisPool jedisPool;//非切片连接池 public static ShardedJedis shardedJedis;//切片额客户端连接 public static ShardedJedisPool shardedJedisPool;//切片连接池 private static void initialPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWait); config.setTestOnBorrow(testOnBorrow); jedisPool = new JedisPool(config, addr, port); } private static void initialShardedPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWait); config.setTestOnBorrow(testOnBorrow); // slave链接 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(addr, port, auth)); // 构造池 shardedJedisPool = new ShardedJedisPool(config, shards); } public static void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub initialPool(); initialShardedPool(); try { shardedJedis = shardedJedisPool.getResource(); } catch (Exception e) { System.out.println("连接shardedJedisPool失败!"); } try { jedis = jedisPool.getResource(); } catch (Exception e) { System.out.println("连接jedisPool失败!"); } } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } public int getMaxActive() { return maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public int getMaxIdle() { return maxIdle; } public void setMaxIdle(int maxIdle) { this.maxIdle = maxIdle; } public int getMaxWait() { return maxWait; } public void setMaxWait(int maxWait) { this.maxWait = maxWait; } public int getTimeOut() { return timeOut; } public void setTimeOut(int timeOut) { this.timeOut = timeOut; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; }}
功能测试
public void selectReviewsByGoodsId(HttpServletRequest request){ try { Jedis jedis = new Jedis(); Date time1 = new Date(); jedis.set("goodsName","IphoneX"); Date time2 = new Date(); System.out.println("消耗时间:"+(time2.getTime()-time1.getTime())); System.out.println("商品列表存入redis完毕"); System.out.println(jedis.get("goodsName")); } catch (Exception e) { //如果缓存连不上,则不处理 System.out.println("登录无法更新该用户缓存"); } }
打印输出:IphoneX
不恰当的地方请指出,不胜感激!
阅读全文
0 0
- java web项目配置Redis(windows环境)
- Windows环境下,Nginx+tomcat+redis实现web项目集群
- Windows环境下,Nginx+tomcat+redis实现web项目集群
- redis sentinel配置(windows环境)
- Windows下配置Redis环境
- Windows 7环境下配置Java Web开发环境
- web项目配置redis缓存
- windows配置Redis环境【Redis · 1】
- redis使用(java web项目 + redis jar)
- centos中安装并配置Java EE开发环境(可以开发java项目和web项目的环境)
- redis在Windows环境配置配置
- Windows下Redis 环境配置 集群配置
- Java Web环境配置
- java web环境配置
- java web环境配置
- Java Web环境配置
- ubuntu下java web项目的环境配置和发布
- eclipse环境 Java web项目的Tomcat配置
- Uvalive 6471 Triangle Count Sequences of Polygon Triangulations 多边形三角剖分 递归思想
- java web中的中文乱码问题和解决方法
- HTML(二十)
- PAT Basic 1032
- Python文件(夹)操作
- java web项目配置Redis(windows环境)
- 扫雷
- oracle总结
- [Hibernate 框架学习]一、初步体验
- highcahrt动态删减series数据列 及 图例点击事件
- SQL基础查询语句(insert update delete)
- jQuery序列化后的表单值转换成Json
- 用于物体追踪的.avi视频文件
- HTML(二十一)