Install Redis and store Java objects in Redis

Source: Internet
Author: User
Tags redis version redis windows install redis

Redis windows:https://github.com/servicestack/redis-windows

One, download the installation package extracted to the specified location of the drive letter, the following structure: (Pure hand knocking, if there is missing, forgive me)

Redis.doc

Redis-benchmark.exe

Redis-check-aop.exe

Redis-check-dump.exe

Redis-cli.exe

Redisqfork_8524.dat

Redis-server.exe

Redisservice.doc

My installation path: D:\install_tool\redis\redis64-2.8.9

Ii. using Windows dos Windows

Open to the installation path, execute this command: Redis-server redis.windows.conf

If a graphical interface appears,

_._
_.-' __ '-._
_.-``    `.  `_. "-._ Redis 2.8.9 (00000000/0)
.-`` .-```. ' \ \ _.,_ '-._
(',.-' | ',) Running in Stand alone mode
| '-._ '-...-' __...-. '-._| '     ' _.-' | port:6379
|     '-._ '. _/_.-' | pid:8524
'-._ '-._ '-./_.-' _.-'
| '-._ '-._ '-.__.-' _.-' _.-' |
|           '-._ '-._ _.-' _.-' | Http://redis.io
'-._ '-._ '-.__.-' _.-' _.-'
| '-._ '-._ '-.__.-' _.-' _.-' |
| '-._ '-._ _.-' _.-' |
'-._ '-._ '-.__.-' _.-' _.-'
'-._ '-.__.-' _.-'
'-._ _.-'
'-.__.-'

[8524] 14:41:49.229 # Server started, Redis version 2.8.9
[8524] 14:41:49.230 * The server is now a ready-to-accept connections on PO
RT 6379

Indicates that the Redis service started successfully. Double-click, Redis-cli.exe Client, test, input, set age 21 back to car, next line, get age enter, if output 21, it means that you installed Redis successfully.

Second, Redis store Java objects

1, every time you want to operate Redis, be sure to remember to open the Redis service, otherwise, will report a connection error.

2, want to operate Java objects stored in Redis, be sure to join Jar,pom as follows:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
<name>Jedis</name>
</dependency>

First initialize the local Redis service, Jedis Jedis = new Jedis ("127.0.0.1", 6379), and store the Java object in the Jedis object's set (parameter 1, Parameter 2) method. Because the function of the Set method is two bytes, the first parameter is the key (bytes) stored in the Redis, the second parameter, the value (Byte object)

1) Create object 2) Convert object to byte 3) into 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 {                     //serialization                           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 {                 //deserialization                  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));       &nbSp; jedis.set ("Person:3". GetBytes (),  serializeutil.serialize (p3));         jedis.set ("Person:4". GetBytes (),  serializeutil.serialize (p4));     } }

Run the Main method, open the Redis client, enter, get Person:1, have the following output, indicating the success of the Save

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>

Install Redis and store Java objects in Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.