nodejs的redis工具类 原

做一个工具时,起初用到redis,后来发现有更好的解决方案,遂放弃redis,但辛辛苦苦写的code不舍得删,这里记录下

nodejs

/**
 * Created by shangchengcai on 2018/1/9.
 */
/**
 * Module dependencies.
 */
var ioredis = require('ioredis');
var redis   = require('redis');
var conf=require("../conf/arconf.json");
var logger=require("../util/LogUtil").LOGGER;
var client;

/**
 * config redis client by redis deploy type
 */
switch (conf.redisDeployType){
    case "single":
        var redisconf=conf.redisAddress[0];
        client=redis.createClient(redisconf.port, redisconf.host);
        break;
    case "cluster":
        client=new ioredis.Cluster(conf.redisAddress);
        break;
    default:
        throw new Error("redisDeployType only can be single or cluster");
        break;

}

/**
 * create redis client err
 */
client.on("error", function(error) {
    logger.error(error);
});

/**
 * get hash key fields
 */
function get(key,callback){
    client.hscan(key,0, function (err, res) {
        callback(err,res[1]);
    });
}

/**
 * remove hash key field
 */
function remove(key,field){
    client.hdel(key,field, function (err, res) {
        if(err){
            logger.info(err);
        }
        logger.info("remove faild host for key="+key+" and field="+field+" excute success!redis return code is "+(res===0?"faild,because redis aready don't contains this key!":"success!"));
    });
}

/**
 * close redis cluster client
 */
function close(){
    client.quit(function(){
        logger.info("close redis success!");
    });
}
function scan(key,callback){
    client.scan("0","MATCH","data_sync.voole.com*",function (err, res) {
        console.log(res);
        callback(err,res[1]);
    });
}

scan("data_*",function(err,data){
    //console.log(data);
    close();
})



/**
 * export get、remove、close
 */
exports.get=get;
exports.remove=remove;
exports.close=close;

java

package com.scc.ar.client.redis;

import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: SchedulerUtil
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author shangchengcai@voole.com
 * @date 2018年1月12日 下午5:39:24
 * 
 */
public class RedisUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
	private static final String TRIGGER_NAME = "AR_CLIENT_TRIGGER";
	private static final String TRIGGER_GROUP = "AR_CLIENT_TRIGGER_GROUP";
	private static final String JOB_NAME = "AR_CLIENT_JOB";
	private static final String JOB_GROUP = "AR_CLIENT_JOB_GROUP";
	private static Scheduler scheduler = null;
	private static RedisUtil schedulerUtil;

	/**
	 * <p>
	 * Title:
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @author shangchengcai@voole.com
	 * @date 2018年1月12日 下午6:07:52
	 */
	private RedisUtil() {
	}

	static {
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
		} catch (SchedulerException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage(), e);
			System.exit(1);
		}
	}

	public static synchronized RedisUtil build() {
		if (null == schedulerUtil) {
			schedulerUtil = new RedisUtil();
		}
		return schedulerUtil;
	}

	public <T extends Job> RedisUtil addJob(Class<T> jobclass, int interval) {
		try {
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity(TRIGGER_NAME, TRIGGER_GROUP).startNow()
					.withSchedule(
							SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(interval).repeatForever())
					.build();
			JobDetail job = JobBuilder.newJob(jobclass).withIdentity(JOB_NAME, JOB_GROUP).build();
			scheduler.scheduleJob(job, trigger);
		} catch (SchedulerException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage(), e);
		}
		return this;
	}

	public void start() {
		try {
			scheduler.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage(), e);
		}
	}

	public void close() {
		try {
			scheduler.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage(), e);
		}
	}

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏菩提树下的杨过

Silverlight:利用异步加载Xap实现自定义loading效果

关键点: 1.利用WebClient的DownloadProgressChanged事件更新下载进度 2.下载完成后,分析Xap包的程序集Assembly信息 ...

20810
来自专栏机器学习从入门到成神

Spring与MyBatis的整合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_35512245/articl...

1.5K2
来自专栏WindCoder

Best Programming Editors? A Never Ending Battle With No Clear Winner

原文:Best Programming Editors? A Never Ending Battle With No Clear Winner

691
来自专栏

Flex 上传文件,服务端.net

using System; using System.Data; using System.Configuration; using System.Collec...

701
来自专栏高性能服务器开发

(三) 服务器端的程序架构介绍2

下面我们以pc端登录为例来具体看一个数据包在服务器端各个服务之间走过的流程: 步骤1:login_server初始化侦听socket,设置新连接到来的回调函数。...

4024
来自专栏菩提树下的杨过

ExtJs+WCF+LINQ实现分页Grid

上篇文章《用ExtJs+Linq+Wcf打造简单grid 》,这个网格控件不带分页,本文在上文的基础上添加分页功能,文中会着重介绍如何在用LINQ返回分页数据,...

3477
来自专栏何俊林

FFmpeg总结(十一)用ffmpeg进行转格式,Android下播放网络音频流

图:杭州西湖 思路: 1、mp3转成pcm(音频数据),ffmpeg做的事 2、OpenSL ES引擎创建AudioPlayer,实际调用了AudioTra...

6335
来自专栏菩提树下的杨过

ExtJs学习笔记(4)_EditorGridPanel(可编辑的网格控件)

这一节,我们将看到ExtJs功能强大的可编辑网格控件,几乎与VS.Net的GridView功能一样了,但是ExtJs的可是纯JS的UI 一.静态示例(改自Ext...

7055
来自专栏Golang语言社区

Go中的依赖注入

I have written a small utility package to handle dependency injection in Go (it'...

2464
来自专栏Jerry的SAP技术分享

如何在SAP CRM里创建和消费Web service

The following steps demonstrates how to expose a function module as a web servic...

1221

扫码关注云+社区

领取腾讯云代金券