redis windows 下载地址:https://github.com/ServiceStack/redis-windows
一、下载的安装包解压到盘符的指定位置,如下结构:(纯手敲,如果有缺失,见谅)
Redis.doc
redis-benchmark.exe
redis-check-aop.exe
redis-check-dump.exe
redis-cli.exe
RedisQFork_8524.dat
redis-server.exe
RedisService.doc
我的安装路径:D:\install_tool\redis\redis64-2.8.9
二、使用windows dos窗口
打开到安装路径,执行此命令:redis-server redis.windows.conf
如果出现一个图形界面,
_._
_.-``__ ‘‘-._
_.-`` `. `_. ‘‘-._ Redis 2.8.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ‘‘-._
( ‘ , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379
| `-._ `._ / _.-‘ | PID: 8524
`-._ `-._ `-./ _.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ | http://redis.io
`-._ `-._`-.__.-‘_.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ |
`-._ `-._`-.__.-‘_.-‘ _.-‘
`-._ `-.__.-‘ _.-‘
`-._ _.-‘
`-.__.-‘
[8524] 07 Aug 14:41:49.229 # Server started, Redis version 2.8.9
[8524] 07 Aug 14:41:49.230 * The server is now ready to accept connections on po
rt 6379
则表示启动redis服务成功。双击,redis-cli.exe客户端,测试,输入,set age 21 回车,下一行,get age回车,如果输出21,就说明你安装redis成功。
二、redis存放JAVA对象
1、每次想操作redis的时候,一定要记得开启redis服务,否则,会报连接错误。
2、想操作JAVA对象存放在redis中,一定要加入jar,pom如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
<name>Jedis</name>
</dependency>
首先初始化本地的redis服务,Jedis jedis = new Jedis("127.0.0.1",6379),用jedis对象的set(参数1,参数2)方法,把java对象存放进去。由于,set方法的函数,是两个字节,第一个参数是存放在redis中的Key(字节),第二个参数Value(字节对象)
1)创建对象 2)把对象转换成字节 3)放入到redis中
package com.liyi.test.util; import java.io.Serializable; public class Person implements Serializable{ private int id ; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { // TODO Auto-generated constructor stub } public Person(int id, String name) { super(); this.id = id; this.name = name; } }
package com.liyi.test.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { //序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { } return null; } public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { //反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { } return null; } }
package com.liyi.test.util; import redis.clients.jedis.Jedis; public class Test { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Person p1 = new Person(1,"testredis1"); Person p2 = new Person(2,"testredis2"); Person p3 = new Person(3,"testredis2"); Person p4 = new Person(4,"testredis2"); jedis.set("person:1".getBytes(), SerializeUtil.serialize(p1)); jedis.set("person:2".getBytes(), SerializeUtil.serialize(p2)); jedis.set("person:3".getBytes(), SerializeUtil.serialize(p3)); jedis.set("person:4".getBytes(), SerializeUtil.serialize(p4)); } }
运行main方法,打开redis客户端,输入,get person:1,有以下输出,说明保存成功
127.0.0.1:6379> get person:1
"\xac\xed\x00\x05sr\x00\x19com.liyi.test.util.PersonDB\x82\xab\xa7\x8c\xbbz\x02\
x00\x02I\x00\x02idL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x01t\x0
0\ntestredis1"
127.0.0.1:6379>