/JedisSDK

A simple/easy way to communicate with Redis server

Primary LanguageJava

简单易用的Redis Java客户端,基于Jedis封装


配置文件详解

<?xml version="1.0" encoding="UTF-8"?>
<servers> <!-- 配置redis服务器信息,可以有多个 -->
	<server id="1" host="127.0.0.1" port="6379" pass=""> <!-- id唯一,host是服务器地址,port是redis端口,pass是redis密码 -->
		<instance database="0"> <!-- 配置每个redis database,可以有多个instance -->
			<!-- 连接池的配置信息 -->
			<!-- 
					maxIdle:最大空闲连接数
					maxTotal:最大连接数
			 -->
			<pool maxIdle="1" maxTotal="5" testOnBorrow="true" testOnReturn="false" timeoutMillis="3000"></pool>
		</instance>
	</server>
</servers>

如何使用

  • 传入配置文件地址,初始化配置文件
RedisServersBean configBean = com.mh.redis.jedis.config.ConfigReader.getRedisConfig(String configPath);
  • 拿到初始化后的对象就可以启用Redis工具了
JedisPlugin.enable(configBean);
  • 初始化之后就可以使用JedisClientFactory获取工具类
JedisClient cli = JedisClientFactory.get("这里填配置文件的server id", 这里填配置文件的database值);
cli.STRINGS.set("key1", "hello world");
String expected = cli.STRINGS.get("key1");
// 更多操作请参看
com.mh.redis.jedis.core.JedisClient
  • 应用退出的时候请一定要调用如下方法来断开与服务器的连接
JedisPlugin.unplug();

一些工具类

  • 先进先出队列
com.mh.redis.jedis.tools.queue.RedisQueue
// sample code
public void testPutToQueue() {
	JedisClient cli = JedisClientFactory.get("1", 0);
	RedisQueue queue = new RedisQueue(cli, "order");
	for (int i = 0; i < 1000; i++) {
		queue.push(String.valueOf(i));
	}
}
public void getFromQueue() {
	JedisClient cli = JedisClientFactory.get("1", 0);
	RedisQueue queue = new RedisQueue(cli, "order");
	while (true) {
		System.out.println(queue.popWhenHasValue());
		System.out.println("waiting...");
	}
}	
  • 计数器
com.mh.redis.jedis.tools.counter.RedisCounter
// sample code
public void testAddPassErrorTimes3() {
	JedisClient cli = JedisClientFactory.get("1", 0);
	RedisCounter counter = new RedisCounter(cli, "passCounter:user1");
	int current = counter.getCurrent();
	System.out.println(current);
	counter.increase(1);
	counter.increase(4);
	current = counter.getCurrent();
	System.out.println(current);
}
  • ID生成器
com.mh.redis.jedis.tools.idgen.IdWorker
// sample code
public void testSingle() {
	JedisClient cli = JedisClientFactory.get("1", 0);
	IdWorker worker = new IdWorker(cli, "RR");
	for (int i = 0; i < 10000; i++) {
		String id = worker.next();
		System.out.println(id + " " + id.length());
	}
}
  • 发布订阅工具类
com.mh.redis.jedis.tools.idgen.AbstractSubscriber
// sample code
public static void main(String[] args) {
		String redisIp = "127.0.0.1";
		int reidsPort = 6379;
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), redisIp, reidsPort);
		System.out.println(String.format("redis pool is starting, redis ip %s, redis port %d", redisIp, reidsPort));
		// extends the java class AbstractSubscriber
		DemoConsumer consumer = new DemoConsumer();
		consumer.start(jedisPool);
		// publish the message
		Publisher publisher = new Publisher(jedisPool, consumer.channelName());
		publisher.pub("hello jason");
}

TODO List

  • 工具类的开发