java程序怎么连接redis
1个回答
展开全部
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询