Java Redis and javaredis

Source: Internet
Author: User
Tags redis version redis server

Java Redis and javaredis

The related concepts of Redis are not introduced. You can also understand Memcached first, and then compare the differences between the two, so you will have an overall impression.

The server usually chooses Linux, and Redis is officially supported by linux. There are a lot of materials to use. You need to download the relevant server programs and decompress and install them. Due to limited capabilities and conditions, I will only briefly introduce how to install and use windows. If you are interested, you can entertain yourself.

Server-side program: https://github.com/ServiceStack/redis-windows.git

If not, come here: http://download.csdn.net/detail/u013283727/8212831

After the download is complete, use cmd to enter the directory of the downloaded file and try the following operations:


Microsoft Windows [Version 6.1.7601]Copyright (c) 2009 Microsoft Corporation.  All rights reserved.C:\>cd redis64-latestC:\redis64-latest>redis-server redis.windows.conf --maxmemory 200m                _._           _.-``__ ''-._      _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._ (    '      ,       .-`  | `,    )     Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 4552  `-._    `-._  `-./  _.-'    _.-' |`-._`-._    `-.__.-'    _.-'_.-'| |    `-._`-._        _.-'_.-'    |           http://redis.io  `-._    `-._`-.__.-'_.-'    _.-' |`-._`-._    `-.__.-'    _.-'_.-'| |    `-._`-._        _.-'_.-'    |  `-._    `-._`-.__.-'_.-'    _.-'      `-._    `-.__.-'    _.-'          `-._        _.-'              `-.__.-'[4552] 01 Dec 13:38:53.147 # Server started, Redis version 2.8.17[4552] 01 Dec 13:38:53.147 * DB loaded from disk: 0.000 seconds[4552] 01 Dec 13:38:53.147 * The server is now ready to accept connections on port 6379



The client uses a java program to connect. Here we will introduce two common methods.

(Jar package directly find maven to: A http://www.mvnrepository.com search out)


1. Redisson

/*** @ Author fcs * Redisson Example */public class RedissonTest {public static void main (String [] args) {// 1. initialize Config config = new Config (); config. setConnectionPoolSize (10); config. addAddress ("127.0.0.1: 6379"); Redisson redisson = Redisson. create (config); System. out. println ("redis connection successful ..... "); // 2. test concurrentMap. When put, it will be synchronized to redis ConcurrentMap <String, String> map = redisson. getMap ("firstMap"); map. put ("changshengfeng", "male"); map. put ("yongtaoliu", "male"); map. put ("qiaozhu", ""); ConcurrentMap resultMap = redisson. getMap ("firstMap"); System. out. println ("resultMap =" + resultMap. keySet (); // closes the redisson connection. shutdown ();}}


2. Jedis

/*** @ Author fcs * test about jedis * Dec 1, 2014 */public class JedisTest {private static Jedis jedis; @ Beforepublic void setup () {jedis = new Jedis ("127.0.0.1", 6379); System. out. println ("The Redis server is connected .... "); // jedis. auth ("admin"); // permission verification}/*** redis storage string */@ Testpublic void testString () {// Add data jedis. set ("name", "fcs"); System. out. println (jedis. get ("name"); // get the result jedis. append ("name", "is handsome"); // splice Jedis. del ("name"); // delete a key System. out. println (jedis. get ("name"); jedis. mset ("name", "changsheng", "age", "22", "qq", "646653132"); // you can specify multiple key-value pairs for jedis. incr ("age"); // Add 1 operation in the voting System. out. println (jedis. get ("name") + "--" + jedis. get ("age") + "--" + jedis. get ("qq");}/*** operation List */@ Testpublic void testList () {jedis. del ("java framework"); System. out. println (jedis. lrange ("java framework", 0,-1); // first save to key java framework Put three pieces of data in jedis. lpush ("java framework", "spring"); jedis. lpush ("java framework", "struts"); jedis. lpush ("java framework", "hibernate"); // retrieve all data jedis. lrange is used to retrieve data by range. The first is key. The second is actually the third is the end position. System. out. println (jedis. lrange ("java framework", 0,-1); jedis. del ("java framework"); jedis. rpush ("java framework", "spring"); jedis. rpush ("java framework", "struts"); jedis. rpush ("java framework", "hibernate"); // retrieve all data j Edis. lrange is used to retrieve data by range. The first is key. The second is actually the third is the end position. System. out. println (jedis. lrange ("java framework", 0,-1);}/*** operation Set */@ Testpublic void testSet () {jedis. sadd ("haha", "why"); jedis. sadd ("haha", "you"); jedis. sadd ("haha", "so"); jedis. sadd ("haha", "diao"); jedis. sadd ("haha ","? "); // Remove jedis. srem (" haha ","? "); System. out. println (" judgement? Is it an element of the haha set: "+ jedis. sismember (" haha ","? "); System. out. println ("Get all added values:" + jedis. smembers ("haha"); System. out. println ("returns a random value of the given set name:" + jedis. srandmember ("haha"); System. out. println ("number of elements returned from the set:" + jedis. scard ("haha");}/*** redis map operation */@ Testpublic void testmap () {Map <String, String> map = new HashMap <String, string> (); map. put ("name", "Xiaolu"); map. put ("sex", "male"); map. put ("email", "haha@fcs.com"); jedis. hmset ("user", map); // It is equivalent to giving map another name List <String> rsmap = jedis. hmet ("user", "name", "sex"); // a variable parameter list is followed by the value System represented by some keys in a map. out. println (rsmap); // deletes a key value in the map. hdel ("user", "email"); System. out. println ("deleted ---- email" + jedis. hmet ("user", "email"); System. out. println ("whether there is a record whose key is user:" + jedis. exists ("user"); System. out. println ("number of values stored in map whose key is user:" + jedis. hlen ("user"); System. out. println ("all keys in the returned map object:" + jedis. hkeys ("user"); System. out. println ("all values in the returned map object:" + jedis. hvals ("user"); // use the Iterator <String> iter = jedis. hkeys ("user "). iterator (); System. out. println ("***************** use the iterator ***************"); while (iter. hasNext () {String key = iter. next (); // cross an object System. out. println (key + ":" + jedis. hmet ("user", key )); // iteration key re-value based on the key}/*** here, after the previous execution, we will directly get the value and try whether the data in the memory is still in the * can be turned off on the server side restart the database and run this method directly. * if there is still data, it indicates that the database is automatically persistent. It has a default persistence Mechanism */@ Testpublic void testNoSet () {Iterator <String> iter = jedis. hkeys ("user "). iterator (); System. out. println ("***************** use the iterator ***************"); while (iter. hasNext () {String key = iter. next (); // cross an object System. out. println (key + ":" + jedis. hmet ("user", key )); // iteration key re-value based on the key} // @ AfterClass can be used to test the entire class. The server program will be closed. // public static void close () {// jedis. shutdown (); // @ After cannot be used; otherwise, the server will be shut down every time a method is executed. // System. out. println ("the connection has been closed ..... ");//}}




At this time, we can see some log records in cmd: (this is the default persistence mechanism, which can be viewed in the redis. windows. conf configuration file)

[3972] 01 Dec 13:59:04.073 * 1 changes in 900 seconds. Saving...[3972] 01 Dec 13:59:04.229 # fork operation complete[3972] 01 Dec 13:59:04.229 * Background saving terminated with success[3972] 01 Dec 14:20:05.127 * 1 changes in 900 seconds. Saving...[3972] 01 Dec 14:20:05.267 # fork operation complete[3972] 01 Dec 14:20:05.267 * Background saving terminated with success[3972] 01 Dec 14:35:06.074 * 1 changes in 900 seconds. Saving...[3972] 01 Dec 14:35:06.204 # fork operation complete[3972] 01 Dec 14:35:06.224 * Background saving terminated with success


Ah, come here first ....




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.