Java getting started with redis

Source: Internet
Author: User
1. In the redis Hello world example, the package here is Jedis. Https://github.com/xetorthio/jedis/downloadsimport the jarpackage into the project and open the redisserver (for details on redis downloading and installation, refer to the preliminary understanding of redis and its installation configuration ). Example of greeting: Jedis = new Jedis ("localhost"); 2: Jedis. Set ("key", "Hello world! "); 3: string value = Jedis. get ("key"); 4: system. out. println (value); test various data structures respectively. out. println ("= string ="); Jedis = new Jedis ("localhost"); // string Jedis. set ("key", "Hello world! "); String value = Jedis. get ("key"); system. out. println (value); // list system. out. println ("= List ="); Jedis. rpush ("messages", "hello how are you? "); Jedis. rpush ("messages", "Fine thanks. i'm having fun with redis. "); Jedis. rpush ("messages", "I showould look into this nosql thing ASAP"); List <string> values = Jedis. lrange ("messages", 0,-1); system. out. println (values); // set system. out. println ("= set ="); Jedis. sadd ("myset", "1"); Jedis. sadd ("myset", "2"); Jedis. sadd ("myset", "3"); Jedis. sadd ("myset", "4"); set <string> setvalues = Jedis. smembe RS ("myset"); system. out. println (setvalues); // sorted set Jedis. zadd ("hackers", 1940, "Alan Kay"); Jedis. zadd ("hackers", 1953, "Richard Stallman"); Jedis. zadd ("hackers", 1965, "yukihiro Matsumoto"); Jedis. zadd ("hackers", 1916, "clude Shannon"); Jedis. zadd ("hackers", 1969, "Linus Torvalds"); Jedis. zadd ("hackers", 1912, "Alan Turing"); setvalues = Jedis. zrange ("hackers", 0,-1); system. out. println (S Etvalues); // hash system. out. println ("= hash ="); Map <string, string> pairs = new hashmap <string, string> (); pairs. put ("name", "akshi"); pairs. put ("Age", "2"); pairs. put ("sex", "female"); Jedis. hmset ("kid", pairs); values = Jedis. hmet ("kid", new string [] {"name", "Age", "sex"}); system. out. println (values); setvalues = Jedis. hkeys ("kid"); system. out. println (setvalues); values = Jedis. hvals ("kid "); System. out. println (values); pairs = Jedis. hgetall ("kid"); system. out. println (pairs); then solve the persistence problem. redis is a mechanism that puts all the data in the memory and needs to be frequently synchronized to the disk to ensure data persistence. Data is stored in the memory. I am really worried about my small machine ~ Turn back to the big data to tune the desktop, and then big... This topic is relatively large. You can write a few articles in the future. Now you are in a hurry to use it. Do you want to get started and solve the problem first. There are two main mechanisms: snapshot and aof (append-only file ). Aof will write logs for each write operation, and the server will recover from those log files when it is on the machine. However, the log files will be very large, and my machine cannot afford them. Snapshots are the default method. By default, snapshots are updated every hour. logs are also written when you manually call save, shutdown, and slave commands. Test save. Use the client to query the inserted content of the Code. The image is still in the memory. Then shut down the server. If it is enabled again, there is still a result. Image verification is because it has been automatically saved for too long, and a new value is inserted with Java code. Continue to shut down the server, restart, and other operations. The image has no value. It turns out that the previous value exists because it is automatically saved and re-inserted (if so, what is the case of overwriting? It seems to be directly overwritten), and then it is saved. Close and restart. Jedis. Set ("newkey", "Hello New World! "); String value = Jedis. get ("newkey"); system. out. println (value); Jedis. save (); image can see the value of newkey and overwrite it. After the save operation is executed, a log backup is performed. Enough. 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.