Example of using Java to connect to redis
Redis is an open source key-value storage tool, redis is usually used Store structured data, because redis keys can include String, hash, listset, and sorted list.
Redisclient supports multiple languages, including: c, C, C#, php, java, python, go and other languages. Just choose the appropriate redis client version type according to your own development language.
What is the use of java redis?
I developed it using java language. For java language, redis client also provides a variety of client support, according to the recommended types In order: Jedis, Redisson, JRedis, JDBC-Redis, RJC, redis-protocol, aredis, lettuce. The first two types are recommended. We use the Redisson type version as redisclient.
Redisson version of redis deployable project construction
1. Create a new maven project
2. Add the following content under the dependencies node of the pom.xml file:
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>
3. After saving the pom.xml, you can start development after the eclispe project is built.
Development example
The following is a demonstration of connecting to the redis server, saving and reading the concurrentMap object, and saving The sample code for reading set objects and saving and reading Queue objects is relatively simple and will not be explained in detail here. The code is as follows:
[java] view plaincopy package com.my.test.redis; import java.util.Queue; import java.util.Set; import java.util.concurrent.ConcurrentMap; import org.redisson.Config; import org.redisson.Redisson; public class RedisExample { /** * @param args */ public static void main(String[] args) { // 1.初始化 Config config = new Config(); config.setConnectionPoolSize(10); config.addAddress("127.0.0.1:6379"); Redisson redisson = Redisson.create(config); System.out.println("reids连接成功..."); // 2.测试concurrentMap,put方法的时候就会同步到redis中 ConcurrentMap<String, Object> map = redisson.getMap("FirstMap"); map.put("wuguowei", "男"); map.put("zhangsan", "nan"); map.put("lisi", "女"); ConcurrentMap resultMap = redisson.getMap("FirstMap"); System.out.println("resultMap==" + resultMap.keySet()); // 2.测试Set集合 Set mySet = redisson.getSet("MySet"); mySet.add("wuguowei"); mySet.add("lisi"); Set resultSet = redisson.getSet("MySet"); System.out.println("resultSet===" + resultSet.size()); //3.测试Queue队列 Queue myQueue = redisson.getQueue("FirstQueue"); myQueue.add("wuguowei"); myQueue.add("lili"); myQueue.add("zhangsan"); myQueue.peek(); myQueue.poll(); Queue resultQueue=redisson.getQueue("FirstQueue"); System.out.println("resultQueue==="+resultQueue); // 关闭连接 redisson.shutdown(); } }
For more Redis related knowledge, please visit Redis usage tutorial Column!
The above is the detailed content of What is the use of java redis. For more information, please follow other related articles on the PHP Chinese website!