標籤:
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>
安裝redis 並把java對象存放在redis中