一、Windows版redis配置与启动
Redis官方并未提供windows版本,我们可以在github上下载windows版,https://github.com/MSOpenTech/redis/releases
下面是redis.windows.conf
- #是否作为守护进程运行
- daemonize no
- #Redis 默认监听端口
- port 6479
- #客户端闲置多少秒后,断开连接
- timeout 300
- #日志显示级别
- loglevel verbose
- #指定日志输出的文件名,也可指定到标准输出端口
- logfile redis.log
- #设置数据库的数量,默认最大是16,默认连接的数据库是0,可以通过select N 来连接不同的数据库
- databases 32
- #Dump持久化策略
- #当有一条Keys 数据被改变是,900 秒刷新到disk 一次
- #save 900 1
- #当有10 条Keys 数据被改变时,300 秒刷新到disk 一次
- save 300 100
- #当有1w 条keys 数据被改变时,60 秒刷新到disk 一次
- save 6000 10000
- #当dump .rdb 数据库的时候是否压缩数据对象
- rdbcompression yes
- #dump 持久化数据保存的文件名
- dbfilename dump.rdb
- ########### Replication #####################
- #Redis的主从配置,配置slaveof则实例作为从服务器
- #slaveof 192.168.0.105 6379
- #主服务器连接密码
- # masterauth <master-password>
- ############## 安全性 ###########
- #设置连接密码
- requirepass admin
- ############### LIMITS ##############
- #最大客户端连接数
- # maxclients 128
- #最大内存使用率
- # maxmemory <bytes>
- ########## APPEND ONLY MODE #########
- #是否开启日志功能
- appendonly no
- # AOF持久化策略
- #appendfsync always
- #appendfsync everysec
- #appendfsync no
- ################ VIRTUAL MEMORY ###########
- #是否开启VM 功能
- #vm-enabled no
- # vm-enabled yes
- #vm-swap-file logs/redis.swap
- #vm-max-memory 0
- #vm-page-size 32
- #vm-pages 134217728
- #vm-max-threads 4
二、Jedis
Jedis是Redis的Java客户端,提供了java对于redis的常用操作。
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.8.0</version>
- </dependency>
- import redis.clients.jedis.Jedis;
- import java.util.*;
- /**
- * Created by shenhongxi on 2016/1/21.
- */
- public class RedisTest {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1", 6479);
- jedis.auth("admin"); // password
- String key = "t";
- jedis.set(key, "javared");
- System.out.println(jedis.get(key));
- jedis.append(key, "&wely");
- System.out.println(jedis.get(key));
- jedis.del(key);
- System.out.println(jedis.get(key));
- Jedis jedis2 = new Jedis("127.0.0.1", 6379);
- jedis2.auth("admin");
- jedis2.set(key, "wely.com");
- System.out.println(jedis2.get(key));
- // 批量插入
- jedis.mset("name", "hongxi", "age", "23", "qq", "1037256085");
- jedis.incr("age");
- System.out.println(jedis.get("name") + "_" + jedis.get("age") + "_" + jedis.get("qq"));
- // map
- Map<String, String> user = new HashMap<String, String>();
- user.put("name", "hongxi");
- user.put("age", "23");
- user.put("qq", "1037256085");
- String hmKey = "user";
- jedis.hmset(hmKey, user);
- List<String> list = jedis.hmget(hmKey, "name", "age", "qq");
- System.out.println(list);
- jedis.hdel(hmKey, "name");
- list = jedis.hmget(hmKey, "name", "age", "qq");
- System.out.println(list);
- System.out.println(jedis.hlen(hmKey));
- System.out.println(jedis.exists(hmKey));
- System.out.println(jedis.hkeys(hmKey));
- System.out.println(jedis.hvals(hmKey));
- Iterator<String> iter=jedis.hkeys("user").iterator();
- while (iter.hasNext()){
- String _key = iter.next();
- System.out.println(_key + ":" + jedis.hmget("user", _key));
- }
- // list
- String listKey = "Java Framework";
- jedis.del(listKey);
- System.out.println(jedis.lrange(listKey, 0, -1));
- jedis.lpush(listKey, "spring");
- jedis.lpush(listKey, "restEasy");
- jedis.lpush(listKey, "mybatis");
- System.out.println(jedis.lrange(listKey, 0, -1));
- jedis.del(listKey);
- jedis.rpush(listKey, "spring");
- jedis.rpush(listKey, "restEasy");
- jedis.rpush(listKey, "mybatis");
- System.out.println(jedis.lrange(listKey, 0, -1));
- // set
- String setKey = "lang";
- Long size = jedis.sadd(setKey, "hongxi", "wely", "norip");
- System.out.println(size);
- jedis.srem(setKey, "norip");
- Set<String> set = jedis.smembers(setKey);
- System.out.println(set);
- System.out.println(jedis.sismember(setKey, "hongxi"));
- System.out.println(jedis.srandmember(setKey));
- System.out.println(jedis.scard(setKey));
- // 排序
- jedis.del("a");
- jedis.rpush("a", "1");
- jedis.lpush("a","6");
- jedis.lpush("a", "3");
- jedis.lpush("a", "9");
- System.out.println(jedis.lrange("a", 0, -1)); // [9, 3, 6, 1]
- System.out.println(jedis.sort("a")); // [1, 3, 6, 9]
- System.out.println(jedis.lrange("a",0,-1)); // [9, 3, 6, 1]
- }
- }
三、JedisPool
Jedis“连接池”在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式。Jedis的连接池设计基于apache commons-pool原生库,仅作了简单的封装。连接池基本参数说明这里不加赘述,下面讲述JedisPool编程设计。
- public class PoolTestMain {
- public static void main(String[] args) {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(32);
- config.setMaxIdle(6);
- config.setMinIdle(0);
- config.setMaxWait(15000);
- config.setMinEvictableIdleTimeMillis(300000);
- config.setSoftMinEvictableIdleTimeMillis(-1);
- config.setNumTestsPerEvictionRun(3);
- config.setTestOnBorrow(false);
- config.setTestOnReturn(false);
- config.setTestWhileIdle(false);
- config.setTimeBetweenEvictionRunsMillis(60000);//一分钟
- config.setWhenExhaustedAction((byte)1);
- JedisPool pool = new JedisPool(config,"127.0.0.1",6379,15000,"0123456",12);
- Jedis client = pool.getResource();//从pool中获取资源
- try{
- client.select(0);
- client.set("k1", "v1");
- System.out.println(client.get("k1"));
- }catch(Exception e){
- pool.returnBrokenResource(client);
- e.printStackTrace();
- }finally{
- pool.returnResource(client);//向连接池“归还”资源,千万不要忘记。
- }
- }
- }
spring配置JedisPool:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxActive" value="32"></property>
- <property name="maxIdle" value="6"></property>
- <property name="maxWait" value="15000"></property>
- <property name="minEvictableIdleTimeMillis" value="300000"></property>
- <property name="numTestsPerEvictionRun" value="3"></property>
- <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
- <property name="whenExhaustedAction" value="1"></property>
- </bean>
- <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
- <!-- config -->
- <constructor-arg ref="jedisPoolConfig"></constructor-arg>
- <!-- host -->
- <constructor-arg value="127.0.0.1"></constructor-arg>
- <!-- port -->
- <constructor-arg value="6379"></constructor-arg>
- <!-- timeout -->
- <constructor-arg value="15000"></constructor-arg>
- <!-- password -->
- <constructor-arg value="0123456"></constructor-arg>
- <!-- database index -->
- <constructor-arg value="12"></constructor-arg>
- </bean>
- </beans>
上述代码,事实上存在设计上尚有改进之处,spring-data中对redis的支持,在编程级别更加轻便。
原文链接:[http://wely.iteye.com/blog/2272560]