RedisX - Redis 线程池实现及用法
LGPL
跨平台
Java
软件简介
RedisX
什么是 redisx?
1、包含几乎所有redis线程池实现以及用法;
2、包含redis Master Slave 主备实现;
3、包含redis 读写分离;
4、包含redis Sentinel 线程池;
6、包含redis Sentinel Salve 线程池;
7、新增线程池对spring支持,和spring完美集成;
8、包含redis Sentinel Sharded Master Slave 线程池;
9、包含redis 对象序列化压缩算法,gz和lzma等;
RedisX 单个Reids线程 用法:
public class TestRedis {
public static void main(String[] args) {
for (int i = 0; i < 5000; i++) {
new RedisThread("threadredis" + i).start();
}
}
}
class RedisThread extends Thread {
private String name;
public RedisThread() {
// TODO Auto-generated constructor stub
}
public RedisThread(String name) {
this.name = name;
}
@Override
public void run() {
try {
for (int i = 0; i < 100; i++) {
System.out.println(PowerfulRedisUtil.setString(this.name + "xxx" + i, "fdsfsdf" + i) + "=" + i + "="
+ this.getName() + this.getId());
sleep(200);
}
for (int i = 0; i < 50; i++) {
System.out.println(PowerfulRedisUtil.del(this.name + "xxx" + i));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RedisX SharedMasterSlaveSentinel 用法:
public class TestRedisUtils
{
public static String value="2017年春运来得早,又赶上火车票预售期由60天调整至30天,购票期相对集中。对准备回家过年的人们而言,回家的火车票还好买吗?";
public static void main(String[] args) throws InterruptedException
{
for (int i = 0; i < 500; i++)
{
new ThreadTest("treahd"+i).start();
}
}
class ThreadTest extends Thread
{
private String name;
public ThreadTest()
{
super();
}
public ThreadTest(String name)
{
super();
this.name = name;
}
@Override
public void run()
{
for (int i = 0; i < 10; i++)
{
String string = RedisSharedMasterSlaveSentinelUtil.setObject(name+i,TestRedisUtils.value+name+i);
System.out.println(string + "=" + i);
}
for (int i = 0; i < 10; i++)
{
String value = RedisSharedMasterSlaveSentinelUtil.getObject(name + i, String.class);
if(null != value)
{
System.out.println(value);
}
long l = RedisSharedMasterSlaveSentinelUtil.del(name + i);
System.out.println(l + "=" + i);
}
}
}
RedisX Redis Cluster 线程池用法参考:
public class TestRedisCluster {
public static void main(String[] args) {
for(int i=0;i<100;i++)
{
new RedisClusterThread("RedisClusterThread"+i,100).start();
}
}
}
class RedisClusterThread extends Thread {
private String name;
private int size;
public RedisClusterThread() {
// TODO Auto-generated constructor stub
}
public RedisClusterThread(String name,int size) {
this.name = name;
this.size = size;
}
@Override
public void run() {
// try {
for (int i = 0; i < this.size; i++) {
System.out.println(RedisClusterUtils.setString(this.name + "xxx" + i, "fdsfsdf" + i) + "=" + i + "="
+ this.getName() + this.getId());
//sleep(200);
}
for (int i = 0; i < this.size; i++) {
System.out.println(RedisClusterUtils.del(this.name + "xxx" + i));
}
// } catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
对spring的支持,包com.redissdk.redis.spring下提供spring的支持,用法参考,master slave
读写分离spring配置文件这里贴出来看不到,大家去项目里面去看吧,在test源码包下面的resource下!
spring配置文件配置可以直接配置线程池或指定一个配置文件配置
JAVA Spring 注解测试DEMO:
@RunWith(SpringJUnit4ClassRunner.class) // 整合
@ContextConfiguration(locations="classpath:applicationContext_masterslave.xml") // 加载配置
public class SpringRedisXMasterSlaveTest
{
@Autowired
private RedisXMasterSlave redisXMsterSlave;
@Autowired
private RedisXMasterSlave redisXMsterSlaveOfConfig;
@Test
public void TestRedisX()
{
String key = "key:_redisx_01_masterslave";
//添加对象
String r_ = redisXMsterSlave.setObject(key, "12356465");
System.out.println("存入返回:"+r_);
//获取对象
String s = redisXMsterSlave.getObject(key, String.class);
System.out.println("缓存取数据:"+ s);
//删除对象
long d_ = redisXMsterSlaveOfConfig.del(key);
System.out.println("删除返回:"+ d_);
}
}