java中redis的使用
来源:互联网 发布:数据汇集平台 编辑:程序博客网 时间:2024/06/06 01:35
import java.util.HashSet;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
public class JadisTest {
@Test
public void testJadisSing(){
//创建Jadis 对象
Jedis jedis = new Jedis("123.206.46.51", 6379);
//调用Jedis 方法,跟redis 指令一样
jedis.set("key1", "Jedis Test");
String result = jedis.get("key1");
System.out.println(result);
//关闭Jedis
jedis.close();
}
/**
* JedisPool
*/
@Test
public void testJedisPool(){
//创建Jedis 连接池对象
JedisPool pool = new JedisPool("123.206.46.51", 6379);
//从连接池中取Jedis对象
Jedis jedis = pool.getResource();
//调用Jedis 方法
String result = jedis.get("key1");
System.out.println(result);
//关闭Jedis
jedis.close();
//关闭连接池
pool.close();
}
/**
* redis 群集
*/
@Test
public void testJedisCluster(){
HashSet<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("123.206.46.51", 7001));
nodes.add(new HostAndPort("123.206.46.51", 7002));
nodes.add(new HostAndPort("123.206.46.51", 7003));
nodes.add(new HostAndPort("123.206.46.51", 7004));
nodes.add(new HostAndPort("123.206.46.51", 7005));
nodes.add(new HostAndPort("123.206.46.51", 7006));
JedisCluster cluster = new JedisCluster(nodes);
cluster.set("key1","100");
String str = cluster.get("key1");
System.out.println(str);
}
/**
* 基于spring 的单机版的 redis
*/
@Test
public void testJedisSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisPool jedisPool = (JedisPool) context.getBean("redisClient");
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.get("key1"));
jedis.close();
jedisPool.close();
}
@Test
public void testJedisCluterSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisCluster cluster = (JedisCluster) context.getBean("redisClient");
System.out.println(cluster.get("key1"));
}
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
public class JadisTest {
@Test
public void testJadisSing(){
//创建Jadis 对象
Jedis jedis = new Jedis("123.206.46.51", 6379);
//调用Jedis 方法,跟redis 指令一样
jedis.set("key1", "Jedis Test");
String result = jedis.get("key1");
System.out.println(result);
//关闭Jedis
jedis.close();
}
/**
* JedisPool
*/
@Test
public void testJedisPool(){
//创建Jedis 连接池对象
JedisPool pool = new JedisPool("123.206.46.51", 6379);
//从连接池中取Jedis对象
Jedis jedis = pool.getResource();
//调用Jedis 方法
String result = jedis.get("key1");
System.out.println(result);
//关闭Jedis
jedis.close();
//关闭连接池
pool.close();
}
/**
* redis 群集
*/
@Test
public void testJedisCluster(){
HashSet<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("123.206.46.51", 7001));
nodes.add(new HostAndPort("123.206.46.51", 7002));
nodes.add(new HostAndPort("123.206.46.51", 7003));
nodes.add(new HostAndPort("123.206.46.51", 7004));
nodes.add(new HostAndPort("123.206.46.51", 7005));
nodes.add(new HostAndPort("123.206.46.51", 7006));
JedisCluster cluster = new JedisCluster(nodes);
cluster.set("key1","100");
String str = cluster.get("key1");
System.out.println(str);
}
/**
* 基于spring 的单机版的 redis
*/
@Test
public void testJedisSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisPool jedisPool = (JedisPool) context.getBean("redisClient");
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.get("key1"));
jedis.close();
jedisPool.close();
}
@Test
public void testJedisCluterSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisCluster cluster = (JedisCluster) context.getBean("redisClient");
System.out.println(cluster.get("key1"));
}
}
阅读全文
0 0
- java中redis的使用
- Java中Redis的使用
- java中使用redis
- Java中使用Redis
- java中使用redis
- Redis-Java中使用
- java中使用redis
- Java 中使用 Redis
- Java 中使用 Redis
- java中redis的使用-基础篇
- 【Redis】在 Java 中使用 redis
- java中使用redis学习
- redis 在Java中使用
- 在java中使用redis
- Java中简单使用Redis
- Java中使用redis的完整实例及常用命令
- java web项目中Spring整合Redis的使用
- Redis在Java中使用的一些简单介绍
- 高级控件(一)
- PHP新增的常用数组声明定义方法和注意细节
- Redis为什么使用单进程单线程方式也这么快?
- 考研之南京大学软件学院
- Python中装饰器的总结
- java中redis的使用
- Oracle那些事(8)-PL/SQL Developer安装
- XML文档基本认识和基于JAVA对简单解析
- Map四种获取key和value值的方法,以及对map中的元素排序
- NestedScrolling详解
- CAS详解
- map containsKey与get方法区别经典总结
- 树莓派控制数字舵机转动
- Android中利用APT生成代码