spring+redis整合入门

1、spring和redis整合,依赖的包需要对应,不然会报错:

<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
		<version>2.1.0</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-redis</artifactId>
		<version>1.0.3.RELEASE</version>
	</dependency>

2、配置文件如下:

<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd">
	
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxActive" value="1000"/>
		<property name="maxIdle" value="10"/>
		<property name="minIdle" value="5"/>
	</bean>
	
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="poolConfig" ref="jedisPoolConfig"/>
		<property name="hostName" value="127.0.0.1"/>
		<property name="port" value="6379"/>
	</bean>
	
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory"/>
	</bean>
</beans>

3、实现:

package com.plateno.web.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;

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.plateno.web.service.RedisService;
@Service("redisService")
public class RedisServiceImpl implements RedisService {

	private static final String ENCODING = "UTF-8";
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Override
	public void save(final String key, final String value) {
		try {
			redisTemplate.execute(new RedisCallback<String>() {
				@Override
				public String doInRedis(RedisConnection connection) throws DataAccessException {
					connection.set(key.getBytes(), value.getBytes());
					return null;
				}
			});
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String get(final String key) {
		try {
			String value = redisTemplate.execute(new RedisCallback<String>() {
				@Override
				public String doInRedis(RedisConnection connection) throws DataAccessException {
					byte[] b = connection.get(key.getBytes());
					try {
						return new String(b, ENCODING);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					return null;
				}
			});
			return value;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Set<String> keySet() {
		Set<String> resultSet = redisTemplate.execute(new RedisCallback<Set<String>>() {
			@Override
			public Set<String> doInRedis(RedisConnection connection) throws DataAccessException {
				try {
					Set<byte[]> set = connection.keys("*".getBytes());
					Set<String> setStr = new HashSet<String>();
					for(byte[] b : set) {
						String str = new String(b,ENCODING);
						setStr.add(str);
					}
					return setStr;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				return null;
			}
		});
		return resultSet;
	}

}

基本上就可以操作redis缓存服务器了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值