spring+redis整合入门

来源:互联网 发布:得力美工刀片型号 编辑:程序博客网 时间:2024/06/09 15:33

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://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";@Autowiredprivate RedisTemplate<String, String> redisTemplate;@Overridepublic void save(final String key, final String value) {try {redisTemplate.execute(new RedisCallback<String>() {@Overridepublic String doInRedis(RedisConnection connection) throws DataAccessException {connection.set(key.getBytes(), value.getBytes());return null;}});} catch(Exception e) {e.printStackTrace();}}@Overridepublic String get(final String key) {try {String value = redisTemplate.execute(new RedisCallback<String>() {@Overridepublic 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;}@Overridepublic Set<String> keySet() {Set<String> resultSet = redisTemplate.execute(new RedisCallback<Set<String>>() {@Overridepublic 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 0