Simple example of Java operation Redis

Source: Internet
Author: User
Tags redis download install redis

First: Installing Redis
First we want to install Redis, just as we operate the database, and we must first create the environment of the database before we operate.
The Redis download can be Baidu, or open the following download link:
Https://github.com/MSOpenTech/redis

Also can download from my Baidu network disk, download link:
http://pan.baidu.com/s/1pJmaZ71

Redis has a Linux version of the Windows version also, we practice the words are generally win version, the operation is relatively simple. After downloading, the compressed file contains the win 32-bit and win 64-bit two versions, their own according to the personal system to choose.

Second: Unzip the file and run the Redis service

Let's start by talking about the effects of several files we see after Redis decompression:
Redis-benchmark.exe: A performance testing tool that can be used to simulate simultaneous m-sets/gets queries sent by n clients (somewhat like the AB pressure tool for Apache).
Redis-check-aof.exe: Update log Check tool
Redis-check-dump.exe: Local Database Check tool
redis-cli.exe: Client Tools
redis-server.exe: Service-side tools
redis.conf: Redis configuration file
Among them, the red bold is what we often use.

Third: Start the Redis service
Two ways to start:
① into the directory where Redis is located , hold down SHIFT, right-click, "Open Command Window Here" or open cmd to your own CD, but this is more troublesome


Input: Redis-server.exe


In this case our Redis service is started, note, do not close the cmd window

In addition, we can specify a parameter file when starting Redis, in the following format:
Redis-server.exe configuration file path
For example:
Redi-server.exe redis.conf
② run directly by configuring environment variables
The specific configuration method is as follows:


Write to your Redis installation directory in the "Variable Value" column. Once you have configured the environment variable, simply open cmd and enter Redis-server.exe directly to start the service, and the Redis installation directory without CD is executed.

IV: Writing Java test code
① New Java Project
② Importing Redis Packages
Redis's jar package download Link:
Jedis-2.1.0.jar Http://pan.baidu.com/s/1mgxCyvu
Commons-pool-1.5.5.jar http://pan.baidu.com/s/1gdGd0Sb
③ Writing code

1  Packagecom.gf.redisdemo_2;2 3 ImportJava.text.SimpleDateFormat;4 Importjava.util.ArrayList;5 Importjava.util.Date;6 ImportJava.util.Iterator;7 Importjava.util.List;8 ImportJava.util.Set;9 Ten ImportRedis.clients.jedis.Jedis; One ImportRedis.clients.jedis.JedisPool; A ImportRedis.clients.jedis.JedisPoolConfig; - ImportRedis.clients.jedis.JedisShardInfo; - ImportRedis.clients.jedis.ShardedJedis; the ImportRedis.clients.jedis.ShardedJedisPool; -  -  Public classTestredis { -      +     //Statement -     PrivateJedis Jedis;//non-slice client connections +     PrivateShardedjedis Shardedjedis;//Slice amount Client Connection A     PrivateJedispool Jedispool;//non-tiled connection pool at     PrivateShardedjedispool Shardedjedispool;//Slice Connection Pool -      -     //Construction -      PublicTestredis () { - Initjedispool (); - Initsharededjedispool (); injedis=Jedispool.getresource (); -shardedjedis=Shardedjedispool.getresource (); to     } +      -     //Initialize Sharededjedispool the     Private voidInitsharededjedispool () { *         //Configuration Parameters $Jedispoolconfig config=Newjedispoolconfig ();Panax NotoginsengConfig.setmaxactive (50); -Config.setmaxidle (5); theConfig.setmaxwait (2000l); +Config.settestonborrow (true); AJedisshardinfo jedisshardinfo=NewJedisshardinfo ("127.0.0.1", 6379);//address, Port theList<jedisshardinfo> list=NewArraylist<jedisshardinfo>(); + List.add (jedisshardinfo); -Shardedjedispool=Newshardedjedispool (config, list); $     } $  -     //Initialize Jedispool -     Private voidInitjedispool () { theJedispoolconfig config=Newjedispoolconfig (); -Config.setmaxactive (50);WuyiConfig.setmaxidle (5); theConfig.setmaxwait (2000l); -Config.settestonborrow (true); Wu         //the same code used to initialize the Sharededjedispool is configured here -Jedispool=NewJedispool (config, "127.0.0.1", 6379); About     } $  -      Public voidShowinfo () { -Date date=NewDate (); -SimpleDateFormat sdf=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); A          +System.out.println (">>>>>>> emptying database:" +Sdf.format (date)); the System.out.println (Jedis.flushdb ()); -System.out.println (">>>>>>>set:" +Sdf.format (date)); $System.out.println (Shardedjedis.set ("name", "Zhangsan")); theSystem.out.println (">>>>>>>get:" +Sdf.format (date)); theSystem.out.println (Shardedjedis.get ("name")); the  theSystem.out.println (">>>>>> Get all Key values:" +Sdf.format (date)); -Set<string> Set=jedis.keys ("*"); inIterator<string> iterator=set.iterator (); the          while(Iterator.hasnext ()) { theString key=Iterator.next (); AboutSystem.out.println (key+ ", Value:" +Shardedjedis.get (key)); the         } the     } the  +      Public Static voidMain (string[] args) { -         NewTestredis (). Showinfo (); the     }Bayi  the}


May just open the time to Jedis, Shardedjedis, Jedispool, Sharededjedispool initialization sequence unclear, easy confusion, for this we declare in accordance with certain rules to declare:

Jedis---shardedjedis jedispool----shardjedispool.

The initialization diagram is as follows:

As can be seen from the diagram, we want to initialize Jedis, Shardedjedis to create Sharedjedis and Shardedjedispool first, so we have Initjedispool () and Initsharededjedispool () Method


Note: If you use the configuration file when you start Redis, and you open the login password in the config file (find redis.conf's "requirepass password" to remove the comment), if you run the above code directly, you will see the error message:
Children

Simple example of Java operation 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.