`

spring-data-redis

    博客分类:
  • java
阅读更多
Spring-data-redis使用
1、maven依赖
 <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${redis.client.version}</version>
        </dependency>
<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>${spring.data.redis}</version>
        </dependency>

2、spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.host}" />
		<property name="port" value="${redis.port}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
	</bean>
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="10000" />
		<property name="maxIdle" value="100" />
		<property name="maxWaitMillis" value="10000" />
		<property name="testOnBorrow" value="true" />
	</bean>
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
		p:connection-factory-ref="connectionFactory">
		<property name="keySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
	</bean>
	<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
		p:connection-factory-ref="connectionFactory">
		<property name="keySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
	</bean>
</beans>	

3、redis引用代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.ppfuns.video.core.util.ExceptionEnum;
import com.ppfuns.video.core.util.ExceptionGlobal;
import com.ppfuns.video.core.util.MessageUtil;


@Service
public class RedisService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	private Lock lock = new ReentrantLock();

	//******************************************String Ops **********************************************
	/**
	 * 
	* @Title: set 
	* @Description: 添加数据 
	* @param key
	* @param value   
	* @return void   
	* @throws
	 */
	public void set(final String key, final String value) {
		this.redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(key.getBytes(), value.getBytes());
				return null;
			}
		});
	}

	/**
	 * 
	* @Title: append 
	* @Description: 追加数据 
	* @param key
	* @param value
	* @return   
	* @return long   
	* @throws
	 */
	public long append(final String key, final String value) {
		long sum = this.redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.append(key.getBytes(), value.getBytes());
			}
		});

		return sum;
	}

	/**
	 * 
	* @Title: set 
	* @Description: 添加带有过期时间的数据 
	* @param key
	* @param value
	* @param seconds   
	* @return void   
	* @throws
	 */
	public void set(final String key, final String value, final long seconds){
		this.redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(key.getBytes(), seconds, value.getBytes());
				return null;
			}
		});
	}

	/***
	 * 
	* @Title: del 
	* @Description: 删除key下数据 
	* @param key
	* @return   
	* @return long   
	* @throws
	 */
	public long del(final String key) {
		long s = this.redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				long sum = connection.del(key.getBytes());
				return sum;
			}
		});

		return s;
	}

	/**
	 * 
	* @Title: del 
	* @Description: 删除数据keys数据 
	* @param keys
	* @return 返回删除数据size  
	* @return long   
	* @throws
	 */
	public long del(final String... keys){
		long s = this.redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				long sum = 0;
				for (String key : keys) {
					sum = sum + connection.del(key.getBytes());
				}

				return sum;
			}
		});

		return s;
	}

	/**
	 * 
	* @Title: get 
	* @Description: 获取数据 
	* @param key
	* @return   
	* @return String   
	* @throws
	 */
	public String get(final String key){
		String value = this.redisTemplate.execute(new RedisCallback<String>() {
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				if (connection.exists(key.getBytes())) {
					byte[] values = connection.get(key.getBytes());
					return redisTemplate.getStringSerializer().deserialize(values);
				}
				return null;
			}
		});
		return value;
	}

	/**
	 * 
	* @Title: size 
	* @Description: 获取key下size 
	* @param key
	* @return   
	* @return long   
	* @throws
	 */
	public long size(final String key){
		long s = this.redisTemplate.execute(new RedisCallback<Long>() {

			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				long sum = connection.sCard(key.getBytes());
				return sum;
			}
		});
		return s;

	}

	/**
	 * 
	* @Title: exist 
	* @Description: 是否存在key 
	* @param key
	* @return true/false  
	* @return boolean   
	* @throws
	 */
	public boolean exist(final String key){

		boolean b = this.redisTemplate.execute(new RedisCallback<Boolean>() {

			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				boolean bool = connection.exists(key.getBytes());
				return bool;
			}
		});

		return b;
	}

	/**
	 * 
	* @Title: keys 
	* @Description: 获取keys 
	* @param pattern
	* @return   
	* @return Set<byte[]>   
	* @throws
	 */
	public List<String> keys(final String pattern){
		return this.redisTemplate.execute(new RedisCallback<List<String>>() {
			@Override
			public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
				Set<byte[]> set = connection.keys(pattern.getBytes());
				List<String> list = new ArrayList<String>();
				for (byte[] b : set) {
					list.add(new String(b));
				}
				return list;
			}
		});
	}

	//******************************************List Ops**************************************************

	/**
	 * 
	* @Title: listSetR 
	* @Description: 左添加数据 
	* @param key
	* @param value
	* @return 添加数据size  
	* @return long   
	* @throws
	 */
	public long listSetR(final String key, final String value){
		return this.redisTemplate.boundListOps(key).rightPush(value);
	}

	/**
	 * 
	* @Title: listSetR 
	* @Description: 添加带有过期时间的数据 
	* @param key
	* @param value
	* @param seconds
	* @return   
	* @return long   
	* @throws
	 */
	public long listSetR(final String key, final String value, final long seconds){
		long sum = this.redisTemplate.boundListOps(key).rightPush(value);
		boolean bool = this.redisTemplate.boundListOps(key).expire(seconds, TimeUnit.SECONDS);
		if (bool) {
			return sum;
		}
		return 0;
	}

	/**
	 * 
	* @Title: listSetR 
	* @Description: 批量添加数据 
	* @param key
	* @param values
	* @return   
	* @return long   
	* @throws InterruptedException
	 */
	public long listSetR(final String key, final String... values) {
		try {
			lock.lockInterruptibly();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			return this.redisTemplate.opsForList().rightPushAll(key, values);
		} finally {
			lock.unlock();
		}

	}

	/**
	 * 
	* @Title: listSetR 
	* @Description: 带有过期时间的批量数据添加 
	* @param key
	* @param seconds
	* @param values
	* @return
	* @throws InterruptedException   
	* @return long   
	* @throws
	 */
	public long listSetR(final String key, final long seconds, final String... values) {
		try {
			lock.lockInterruptibly();
			long sum = 0;
			try {
				sum = this.redisTemplate.boundListOps(key).rightPushAll(values);
				boolean bool = this.redisTemplate.boundListOps(key).expire(seconds, TimeUnit.SECONDS);
				if (bool) {
					return sum;
				}
			} finally {
				lock.unlock();
			}
		} catch (InterruptedException e) {
			MessageUtil.sendMessage(ExceptionEnum.ERROR, "BlueRayCore.RedisService.listSetR", ExceptionGlobal.getExceptionInfo(e));
		}
		return 0;
	}

	/**
	 * 
	* @Title: listDel 
	* @Description: 删除数据 
	* @param key
	* @return   
	* @return boolean   
	* @throws
	 */
	public boolean listDel(final String key){
		this.redisTemplate.boundListOps(key).expire(1, TimeUnit.MILLISECONDS);
		try {
			Thread.sleep(2);
		} catch (InterruptedException e) {
			MessageUtil.sendMessage(ExceptionEnum.ERROR, "BlueRayCore.RedisService.listDel", ExceptionGlobal.getExceptionInfo(e));
		} finally {

		}
		return this.redisTemplate.boundListOps(key).persist();
	}

	/**
	 * 
	* @Title: listGet 
	* @Description: 获取集合数据 
	* @param key
	* @return   
	* @return String   
	* @throws
	 */
	public String listGet(final String key){
		return this.redisTemplate.boundListOps(key).leftPop();

	}

	/**
	 * 
	* @Title: listSize 
	* @Description: 获取集合大小 
	* @param key
	* @return   
	* @return long   
	* @throws
	 */
	public long listSize(final String key){
		return this.redisTemplate.boundListOps(key).size();
	}

}

分享到:
评论

相关推荐

    spring-data-redis-2.6.1-API文档-中文版.zip

    赠送jar包:spring-data-redis-2.6.1.jar; 赠送原API文档:spring-data-redis-2.6.1-javadoc.jar; 赠送源代码:spring-data-redis-2.6.1-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.6.1.pom; ...

    spring-data-redis-2.3.9.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-redis-2.3.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...

    spring-data-redis-2.5.5-API文档-中英对照版.zip

    赠送jar包:spring-data-redis-2.5.5.jar; 赠送原API文档:spring-data-redis-2.5.5-javadoc.jar; 赠送源代码:spring-data-redis-2.5.5-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.5.5.pom; ...

    spring-data-redis-2.0.9.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-data-redis-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...

    spring-data-redis-2.5.5-API文档-中文版.zip

    赠送jar包:spring-data-redis-2.5.5.jar; 赠送原API文档:spring-data-redis-2.5.5-javadoc.jar; 赠送源代码:spring-data-redis-2.5.5-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.5.5.pom; ...

    spring-data-redis-2.0.6.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-redis-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...

    spring-data-redis-1.7.5.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-data-redis-1.7.5.RELEASE.jar 赠送原API文档:spring-data-redis-1.7.5.RELEASE-javadoc.jar 赠送源代码:spring-data-redis-1.7.5.RELEASE-sources.jar 包含翻译后的API文档:spring-data-...

    spring-data-redis-2.0.9.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-redis-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-redis-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...

    spring-data-redis-2.6.1-API文档-中英对照版.zip

    赠送jar包:spring-data-redis-2.6.1.jar; 赠送原API文档:spring-data-redis-2.6.1-javadoc.jar; 赠送源代码:spring-data-redis-2.6.1-sources.jar; 赠送Maven依赖信息文件:spring-data-redis-2.6.1.pom; ...

    spring-data-redis-1.7.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-redis-1.7.5.RELEASE.jar; 赠送原API文档:spring-data-redis-1.7.5.RELEASE-javadoc.jar; 赠送源代码:spring-data-redis-1.7.5.RELEASE-sources.jar; 包含翻译后的API文档:spring-...

    spring-data-redis-1.8.1.RELEASE-sources.jar(源码)

    spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()

    spring-data-redis-1.7.2.RELEASE.jar

    jedis、spring-redis-datade的整合使用,如果版本不匹配可能存在不兼容的问题,从而产生异常。 这里给出无异常的版本匹配: 1、spring-data-redis-1.7.2.RELEASE....这里先提供spring-data-redis-1.7.2.RELEASE.jar

    spring-data-redis 1.7.6

    10. **源码分析**:`spring-data-redis-1.7.6.RELEASE-sources.jar`包含了源码,对于开发者来说,这是一个宝贵的资源,可以深入理解其内部实现,提高解决问题的能力。 总的来说,Spring Data Redis 1.7.6版本提供了...

    spring-data-redis2.jar

    spring-data-redis-2.0.0.M1.jar

    spring-data-redis-1.4.1.RELEASE

    spring-data-redis-1.4.1.RELEASE

    spring-data-redis-1.6.2.RELEASE.jar

    spring-data-redis-1.6.2.RELEASE.jar,官网文档,免费提供,亲测可用

    springMVC集成spring-data-redis

    而Spring Data Redis是Spring Framework的一个模块,专门用于简化与Redis键值存储系统的交互。Redis是一个高性能的、开源的、支持网络的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 在...

    maven版spring-data-redis简单示例

    基于spring的子项目spring-data-redis写的一个基于hash类型的用户CRUD,经过简单的封装,可以实现通用CRUD,请事先安装好redis,配置文件redis.properties请做相应修改,希望对你有帮助。

    spring-session-data-redis-2.0.4.RELEASE-API文档-中文版.zip

    赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...

Global site tag (gtag.js) - Google Analytics