`

REDISHELPER

 
阅读更多

package saas.framework.cache;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import saas.framework.cache.redis.cmds.RedisCmdsFactory;

import saas.framework.service.SpringContextHolder;

 

public class RedisCmdHelper {

 

private int db = 3;

 

private static ThreadLocal<Jedis> jedisPoolCache = new ThreadLocal<Jedis>();

 

private static final Logger logger = LoggerFactory.getLogger(RedisCmdHelper.class);

 

private JedisPool jedisPool;

 

private RedisCmdsFactory redisCmdsFactory;

 

public RedisCmdHelper() {

this.redisCmdsFactory = SpringContextHolder.getBean("redisCmdsFactory");

}

 

public void setJedisPool(JedisPool jedisPool) {

this.jedisPool = jedisPool;

}

 

public void setDb(int db) {

this.db = db;

}

 

public Object executeCallBack(JedisCallback jedisCallback) {

Jedis jedis = null;

boolean isError = false;

try {

jedis = getJedis();

Object obj = jedisCallback.executCmd(jedis);

return obj;

} catch (Exception ex) {

isError = true;

logger.error("execute cmd error", ex);

return null;

} finally {

if (jedis != null){

returnConnection(jedis, isError);

}

jedisPoolCache.remove();

}

}

 

public Object doRedisCmd(String cmd, Object key, Object[] params, Object[] extraParam) {

if (redisCmdsFactory == null) {

redisCmdsFactory = SpringContextHolder.getBean("redisCmdsFactory");

}

if (redisCmdsFactory == null) {

logger.error("redisCmdsFactory cant be null");

}

Object result = null;

Jedis jedis = null;

boolean isError = false;

try {

jedis = getJedis();

jedisPoolCache.set(jedis);

IRedisCmd redisCmd = redisCmdsFactory.getRedisCmdByKey(cmd);

if (redisCmd != null) {

result = redisCmd.exeute(key, params, extraParam);

}

} catch (Exception ex) {

logger.error("execute cmd error", ex);

isError = true;

} finally {

if (jedis != null) {

returnConnection(jedis, isError);

}

jedisPoolCache.remove();

}

return result;

}

 

public Object doRedisCmd(String cmd, Object key, Object[] params) {

return doRedisCmd(cmd, key, params, null);

}

 

public Object doRedisCmd(String cmd, Object key) {

return doRedisCmd(cmd, key, null, null);

}

 

private Jedis getJedis() {

Jedis jedis = jedisPool.getResource();

jedis.select(db);

return jedis;

}

 

private void returnConnection(Jedis jedis, Boolean error) {

if (error) {

jedisPool.returnBrokenResource(jedis);

} else {

jedisPool.returnResource(jedis);

}

}

 

public static Jedis getCurrentJedis() {

return jedisPoolCache.get();

}

}

----------------------------------------------------------------------------------

 

/**

 * Copyright (c) 2005-2010 springside.org.cn

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * 

 * $Id: SpringContextHolder.java 1211 2010-09-10 16:20:45Z calvinxiu $

 */

package saas.framework.service;

 

 

/**

 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.

 * 

 */

public class SpringContextHolder {

 

 

/**

* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.

*/

@SuppressWarnings("unchecked")

public static <T> T getBean(String name) {

return (T) ServiceLocator.getService(name);

}

 

public static <T> T getBean(Class<T> interfaceClass) {

return (T) ServiceLocator.getService(interfaceClass);

}

}

 

----------------------------------------------------------------------------------

package saas.framework.service;

 

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

 

/**

 *star

 */

@Component

public class ServiceLocatorInitializer implements ApplicationContextAware {

 

/* (non-Javadoc)

* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)

*/

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

ServiceLocator.setApplicationContext(applicationContext);

}

 

}

-----------------------------------------------------------------------------------------------

 

package saas.framework.service;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.MessageSource;

 

public class ServiceLocator {

private static final Logger logger = LoggerFactory.getLogger(ServiceLocator.class);

 

private static ApplicationContext applicationContext;

 

/**

* @return Returns the applicationContext.

*/

public static synchronized ApplicationContext getApplicationContext() {

return applicationContext;

}

 

/**

* @param applicationContext

*            The applicationContext to set.

*/

public static synchronized void setApplicationContext(ApplicationContext appContext) {

applicationContext = appContext;

}

 

/**

* Returns the service that is associated with the interface whose class you

* provide. For right now, that's going to be the bean in the Spring context

* whose name is the class name with the first letter lowercased (e.g.

* SecurityDAO.class looks up a bean called "securityDAO"). Changed this so

* that it just caches the name of the service rather than the service

* itself.

* @param interfaceClass

*            the class of the interface you wish to return.

* @return the appropriate object, or null if it doesn't exist.

*/

public static <T> T getService(Class<T> interfaceClass) {

if (interfaceClass == null) {

return null;

}

return applicationContext.getBean(interfaceClass);

}

 

/**

* Returns the service corresponding to the name you request. This is

* private now because we want to force people to get beans according to the

* interface, and not let them ask for any old bean.

* @param serviceName

*            the name of the service to obtain.

* @return the appropriate service object, or null if none can be found.

*/

public static Object getService(String serviceName) {

try {

if (applicationContext == null) {

return null;

}

 

Object o = applicationContext.getBean(serviceName);

return o;

} catch (NoSuchBeanDefinitionException e) {

logger.warn("No bean found with name:" + serviceName);

return null;

}

}

 

/**

* Returns the service of particular type

* @param serviceName

* @param requiredType

* @return

*/

public static <T> T getService(String serviceName, Class<T> requiredType) {

if (applicationContext == null) {

return null;

}

return applicationContext.getBean(serviceName, requiredType);

}

 

public static MessageSource getMessageSource() {

return getApplicationContext();

}

 

}

-------------------------------------------------------------------------------------

package saas.framework.cache.redis.cmds;

 

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

 

import org.springframework.stereotype.Component;

 

import saas.framework.cache.IRedisCmd;

 

@Component

public class RedisCmdsFactory {

 

private Map<String, IRedisCmd> cmdMaps=new ConcurrentHashMap<String, IRedisCmd>();

 

public IRedisCmd getRedisCmdByKey(String key){

return cmdMaps.get(key);

}

 

public void registCmd(IRedisCmd redisCmd){

cmdMaps.put(redisCmd.getCmdId(), redisCmd);

}

}

 

分享到:
评论

相关推荐

    RedisHelper

    RedisHelper是针对Redis数据库操作的一个辅助类库,它在项目中起到了简化Redis操作的作用,使得开发者可以更加便捷地进行数据的存储和检索。Redis是一种高性能的键值对内存数据库,常用于缓存、消息队列等场景,因其...

    C# RedisHelper 类

    C# RedisHelper 类

    RedisHelper Redis的API帮助文件

    RedisHelper 方便使用 Redis的好的方式,封装了大多数API使其开发更加的方便。

    redisHelper

    在C#环境中,为了方便地操作Redis,通常会封装一个辅助类,这里我们称其为`RedisHelper`。这个类的主要作用是提供一套统一的、易于使用的API,使得开发者在C#代码中可以轻松地进行数据的读写、删除等操作。 `...

    C# redisHelper

    本文将深入探讨C#中的RedisHelper类,它是连接和操作Redis的关键工具。 首先,`C# redisHelper`指的是一个用C#编写的辅助类,它的主要目标是简化与Redis服务器的通信,提供易于使用的API接口来执行增、删、改、查等...

    RedisHelper.cs

    .net core redishelper 封装类,下载引用可以直接使用,亲测比较好用,比较稳定,生产环境以及搭建

    oppoic-RedisHelper-master.zip

    `OppoIC-RedisHelper-master.zip`是一个关于如何在C#/.NET项目中使用Redis的示例项目,其中包含了详细教程和实例。 首先,`RedisHelper`类是这个项目的核心部分,它封装了与Redis服务器交互的各种操作。在C#中,...

    干掉RedisHelper,请这样用分布式缓存.doc

    在Asp.Net Core项目中,使用传统的RedisHelper静态类来管理分布式缓存的方式已经不再符合最佳实践。这种做法虽然方便快捷,但不支持依赖注入,无法充分利用Asp.Net Core容器的功能,例如动态获取配置信息。因此,...

    RedisHelper.py

    用Python封装的一个redis工具类,包含了常用的string,list,set等;支持redis的订阅发布

    RedisHelper.rar

    在IT行业中,数据库缓存是提高应用程序性能的关键技术之一,特别是在高并发的场景下。本话题将详述如何使用C#结合StackExchange.Redis库来调用Redis作为数据缓存,并与MySQL数据库协同工作。Redis是一个高性能的键值...

    csredis:用于Redis和Redis Sentinel(2.8)和群集的.NET Core或.NET Framework 4.0+客户端。 包括同步和异步客户端

    特征CSRedisClient和RedisHelper保持所有方法名称与redis-cli一致支持地理类型命令(需要redis-server 3.2或更高版本) 支持Redis集群redis-trib.rb 支持Redis Sentinel和主从支持流类型命令(需要redis-server 5.0...

    node-redis-helper:基于承诺的NodeRedis包装器和帮助器功能

    节点重用助手轻量级的基于...redis = new RedisHelper ( redisOptions ) ;取得客户node-redis-helper尝试成为redis . client ( ). then ( function ( client ) {//use client like normalclient . set ( 'chicken' , 'b

    fill_redis.zip_python_redis_redis Python

    本教程将详细介绍如何使用Python的`redis-py`库封装一个名为`RedisHelper`的类,以便更方便地操作Redis。 首先,我们需要安装`redis-py`库。通过Python的包管理器pip,可以轻松完成安装: ```bash pip install ...

    C# winform中使用Redis

    在C# WinForm应用中使用Redis作为数据存储和缓存工具是一种常见的技术实践。Redis,全称Remote Dictionary Server,是一个高性能的键值数据库,支持多种数据结构如字符串、哈希、列表、集合和有序集合等,广泛应用于...

    C# Winfrom的Redis使用Demo

    接下来,我们创建一个RedisHelper类,这是描述中提到的"Helper"。这个类通常包含连接Redis服务器、执行命令以及处理结果的方法。以下是一个简单的RedisHelper类示例: ```csharp using StackExchange.Redis; ...

    c#帮助类文件包含常用的基础C#类

    C#读取AD域里用户名或组,FTP操作类,Excel操作类,Chart图形,H5-微信,JSON操作,JS操作,RDLC直接打印帮助类,RedisHelper,SqlHelper,SQL语句拦截器,URL的操作类,XML操作类,处理多媒体的公共类,处理枚举类...

    C# StackExchange.Redis 操作封装类库

    【实例简介】 C# StackExchange.Redis 操作封装类库,分别封装了Redis五大数据结构(String,Hash,List,Set,ZSet)的增删改查的操作方法,支持Async异步操作。​支持Redis分库操作。支持信息队列操作。...

    C# 基础类大全_CN.zip

    微信、Html操作类、INI文件读写类、IP辅助类、Javascript、Json、JSON操作、JS操作、Lib、Mime、MongoDBHelper、Net、NPOI、obj、packages、Path、PDF...QueryString地址栏参数、RDLC直接打印帮助类、RedisHelper等88项

Global site tag (gtag.js) - Google Analytics