您现在的位置是:首页 > 文章详情

redis安装,redis项目以来,redis和spring整合,redis的service,redis的service实现类

日期:2018-06-21点击:453

redis安装,redis项目以来,redis和spring整合,redis的service,redis的service实现类

一.redis安装:

Redis-3.2.6.tar.gz安装,参考方式:

用源码工程来编译安装

1、  到官网下载最新stable版,这里使用的是:redis-3.2.6.tar.gz

2、  cd /usr/local

3、  make redis-src

4、  tar -zxvf    redis-3.2.6.tar.gz  -C  ./redis-src/

2、解压源码并进入目录cd  /usr/local/redis-src/redis-3.2.6

3、 先执行make,检查是否报错

如果报错提示缺少gcc,则安装gcc :  yum install -y gcc

如果报错提示:Newer version ofjemalloc required

则在make时加参数:make MALLOC=libc (如果没有报错,直接使用make命令)

 

4、安装redis,指定安装目录,如 /usr/local/redis

make PREFIX=/usr/local/redis install

 

5、拷贝一份配置文件到安装目录下

切换到源码目录,里面有一份配置文件redis.conf,然后将其拷贝到安装路径下

cp redis.conf /usr/local/redis/

 

6、启动redis

cd /usr/local/redis

bin/redis-server redis.conf   (如果想后台进程运行,修改:daemonize yes)

 

vim redis.conf 将下面的变量修改为:

daemonize yes

 

将bind的id换成真实的ip地址,比如:

bind 192.168.1.1

 

在集群配置中,要对redis配置密码,修改的配置是将redis.conf这个文件的下面的变量配置成如下的:

requirepass cloudTplWebapp    (这里设置一个密码:cloudTplWebapp)

7、连接redis

另开一个xshell,然后:

#cd /usr/local/redis/

[root@Hadoop redis]# bin/redis-cli

127.0.0.1:6379>

 

二.配置Maven依赖

<!-- 整合redis所需的jar包 -->

        <dependency>

          <groupId>org.springframework.data</groupId>

          <artifactId>spring-data-redis</artifactId>

          <version>1.6.0.RELEASE</version>

      </dependency>

      <dependency>

          <groupId>redis.clients</groupId>

          <artifactId>jedis</artifactId>

          <version>2.7.3</version>

      </dependency>

二.配置applicationContext.xml

<?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"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xmlns:task="http://www.springframework.org/schema/task"

   xsi:schemaLocation=

    http://www.springframework.org/schema/beans  

    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

    http://www.springframework.org/schema/tx  

    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

    http://www.springframework.org/schema/cache

    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

    http://www.springframework.org/schema/task

    http://www.springframework.org/schema/task/spring-task-3.1.xsd

    http://www.springframework.org/schema/context  

    http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   <!-- 引入jdbc配置文件 -->

   <context:property-placeholder location="classpath:jdbc.properties,classpath:redis.properties" />

   <!—在自己的application中加入如下配置 -->

   <import resource="redis-context.xml"/>

</beans>

其中redis-context.xml的内容如下:

<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <!-- Jedis线程 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <!-- 配置多个redis的方式,start --> <bean id="jedis.shardInfo.0" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.ip.0}" /> <constructor-arg index="1" value="${redis.port.0}" /> </bean> <!-- <bean id="jedis.shardInfo.1" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.ip.1}" /> <constructor-arg index="1" value="${redis.port.1}" /> </bean> --> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <ref bean="jedis.shardInfo.0" /> <!--<ref bean="jedis.shardInfo.1" />--> </list> </constructor-arg> </bean> <!-- 配置多个redis的方式,end --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- 以下是配置多个redis的方式 :start --> <!--<property name="shardInfo" value="shardedJedisPool"></property>--> <!-- 以上是配置多个reids的方式 :end --> <!-- 以下是配置单个redis的方式:start --> <property name="hostName" value="${redis.ip.0}" /> <property name="port" value="${redis.port.0}" /> <property name="password" value="${redis.pass}"/> <property name="timeout" value="${redis.timeout}"/> <property name="database" value="${redis.default.db}"/> <property name="poolConfig" ref="jedisPoolConfig" /> <!-- 以上是配置单个redis的方式:end--> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> <bean id="redisService" class="xx.tpl.rediscache.impl.RedisServiceImpl"> <property name="redisTemplate" ref="redisTemplate" /> <property name="expirationtime" value="${redis.expiration}"/> </bean> </beans> 

 

编写redis.properties属性文件

#redis.pool.maxActive=1024

#redis.pool.maxIdle=200

#redis.pool.maxWait=1000

#redis.pool.testOnBorrow=true

redis.ip.0=192.168.1.1

redis.port.0=6379

#密码

redis.pass=cloudTplWebapp

#设置默认的连接的db

redis.default.db=0

redis.timeout=100000

redis.maxActive=300

redis.ip.1=192.168.1.1

redis.port.1=6379

redis.pool.maxTotal=1024

redis.pool.maxIdle=200

redis.pool.maxWaitMillis=1000

redis.pool.testOnBorrow=true

redis.expiration=300

 

Redis的RedisService如下:

package xxx.xxx.xxx.rediscache;

 

import java.io.Serializable;

import java.util.Map;

import java.util.Set;

 

/**

* RedisService.java redis的服务操作类

* @attention

* @author toto

* @date 2017-1-12

* @note begin modify by 涂作权 2017-1-12 原始创建

*/

public interface RedisService<T> {

 

/**

* 将内容保存到缓存中

*

* @param key

* @param value

* @throws Exception

* @attention

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12 原始创建

*/

public void save(final String key, Object value) throws Exception;

 

/**

* 获取内存中的内容

* @param <T>

* @param key

* @param elementType

* @return

* @throws Exception

* @attention

* @author toto

* @date 2017-1-12

* @note  begin modify by 原始创建 2017-1-12  原始创建

*/

public <T> T getByKey(final String key, Class<T> elementType) throws Exception;

 

/**

* 通过传递key删除所有的在缓存中的内容

* @param key

* @attention

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public void del(String... key) throws Exception;

 

/**

* 批量删除<br/>

* @param pattern

* @throws Exception

* @attention 该操作会执行模糊查询,请尽量不要使用,以免影响性能或误删

* @author toto

* @date 2017-1-12

* @note begin modify by 涂作权  2017-1-12  原始创建

*/

public void batchDel(String... pattern) throws Exception;

 

/**

* 取得缓存(int型)

* @param key

* @return

* @throws Exception

* @attention 方法的使用注意事项

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12 原始创建

*/

public Integer getInt(String key) throws Exception;

 

/**

* 取得缓存(字符串类型)

* @param key

* @return

* @attention 方法的使用注意事项

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public String getStr(String key) throws Exception;

 

/**

* 取得缓存(字符串类型)

* @param key

* @param retain

* @return

* @throws Exception

* @attention

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public String getStr(String key,boolean retain) throws Exception;

 

/**

* 获取缓存<br>

* @param key

* @return

* @throws Exception

* @attention 注:基本数据类型(Character除外),请直接使用get(String key, Class<T> clazz)取值

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public Object getObj(String key) throws Exception;

 

/**

* 获取缓存<br>

* @param key

* @param retain 是否保留

* @return

* @attention 注:java 8种基本类型的数据请直接使用get(String key, Class<T> clazz)取值

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12 原始创建

*/

public Object getObj(String key,boolean retain) throws Exception ;

 

/**

* 获取缓存

* @param <T>

* @return

* @throws Exception

* @attention 方法的使用注意事项

* @author toto

* @date 2017-1-12

* @note  begin modify by 修改人 修改时间   修改内容摘要说明

*/

public <T> T get(String key, Class<T> clazz) throws Exception;

 

/**

* 将value对象写入缓存

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void set(String key,Object value,long expirationtime)throws Exception;

 

/**

* 递减操作

* @param key

* @param by

* @return

*/

public double decr(String key, double by)throws Exception;

 

/**

* 递增操作

* @param key

* @param by

* @return

*/

public double incr(String key, double by)throws Exception;

 

/**

* 获取double类型值

* @param key

* @return

*/

public double getDouble(String key) throws Exception;

 

/**

* 设置double类型值

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void setDouble(String key, double value,long expirationtime) throws Exception;

 

/**

* 设置double类型值

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void setInt(String key, int value, long expirationtime) throws Exception;

 

/**

* 将map写入缓存

* @param key

* @param map

* @param time 失效时间(秒)

*/

public <T> void setMap(String key, Map<String, T> map, long expirationtime)throws Exception;

 

/**

* 向key对应的map中添加缓存对象

* @param key

* @param map

*/

public <T> void addMap(String key, Map<String, T> map)throws Exception;

 

/**

* 向key对应的map中添加缓存对象

* @param key   cache对象key

* @param field map对应的key

* @param value     值

*/

public void addMap(String key, String field, String value)throws Exception;

 

/**

* 向key对应的map中添加缓存对象

* @param key   cache对象key

* @param field map对应的key

* @param obj   对象

*/

public <T> void addMap(String key, String field, T obj)throws Exception;

 

/**

* 获取map缓存

* @param key

* @param clazz

* @return

*/

public <T> Map<String, T> mget(String key, Class<T> clazz)throws Exception;

 

/**

* 获取map缓存中的某个对象

* @param key

* @param field

* @param clazz

* @return

*/

public <T> T getMapField(String key, String field, Class<T> clazz)throws Exception;

 

/**

* 删除map中的某个对象

* @author lh

* @date 2016年8月10日

* @param key   map对应的key

* @param field map中该对象的key

*/

public void delMapField(String key, String... field)throws Exception;

 

/**

* 指定缓存的失效时间

*

* @author FangJun

* @date 2016年8月14日

* @param key 缓存KEY

* @param time 失效时间(秒)

*/

public void expire(String key, long expirationtime) throws Exception;

 

/**

* 添加set

* @param key

* @param value

*/

public void sadd(String key, String... value) throws Exception;

 

/**

* 删除set集合中的对象

* @param key

* @param value

*/

public void srem(String key, String... value) throws Exception;

 

/**

* set重命名

* @param oldkey

* @param newkey

*/

public void srename(String oldkey, String newkey)throws Exception;

 

/**

* 模糊查询keys

* @param pattern

* @return

*/

public Set<Serializable> keys(String pattern)throws Exception;

}

 

Redis的RedisServiceImpl的实现类:

package xxx.xxx.xxx.rediscache.impl;

 

import java.io.Serializable;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.TimeUnit;

 

import org.apache.commons.lang.StringUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.dao.DataAccessException;

import org.springframework.data.redis.connection.RedisConnection;

import org.springframework.data.redis.core.BoundHashOperations;

import org.springframework.data.redis.core.RedisCallback;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.util.CollectionUtils;

 

import com.ucap.tpl.rediscache.RedisService;

import com.ucap.tpl.utils.SerializeUtils;

 

@SuppressWarnings({"unchecked"})

public class RedisServiceImpl<T> implements RedisService<T> {

@SuppressWarnings("unused")

private static Log logger = LogFactory.getLog(RedisServiceImpl.class);

private static RedisTemplate<Serializable, Serializable> redisTemplate;

private long expirationtime;

 

public long getExpirationtime() {

return expirationtime;

}

 

public RedisTemplate<Serializable, Serializable> getRedisTemplate() {

return redisTemplate;

}

 

@SuppressWarnings("static-access")

public void setRedisTemplate(

RedisTemplate<Serializable, Serializable> redisTemplate) {

this.redisTemplate = redisTemplate;

}

 

public void setExpirationtime(long expirationtime) {

this.expirationtime = expirationtime;

}

 

/**

* 保存内容到缓存中

*/

public void save(final String key, Object value) throws Exception {

final byte[] vbytes = SerializeUtils.serialize(value);

redisTemplate.execute(new RedisCallback<Object>() {

 

public Object doInRedis(RedisConnection connection)

throws DataAccessException {

connection.set(redisTemplate.getStringSerializer().serialize(key), vbytes);

return null;

}

});

}

 

/**

* 通过key取到保存在缓存中的内容

*/

public <T> T getByKey(final String key, Class<T> elementType) throws Exception {

try {

return redisTemplate.execute(new RedisCallback<T>() {

public T doInRedis(RedisConnection connection)

throws DataAccessException {

byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);

if (connection.exists(keyBytes)) {

byte[] valueBytes = connection.get(keyBytes);

 

T value = (T) SerializeUtils.unserialize(valueBytes);

return value;

}

return null;

}

});

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

 

/**

* 通过传递key删除所有的在缓存中的内容

* @param key

* @attention 这里个的key是可变参数的key

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public void del(String... key) throws Exception {

if (key != null && key.length > 0) {

if (key.length == 1) {

redisTemplate.delete(key[0]);

} else {

redisTemplate.delete(CollectionUtils.arrayToList(key));

}

}

}

 

/**

* 批量删除<br/>

* @param pattern

* @throws Exception

* @attention 该操作会执行模糊查询,请尽量不要使用,以免影响性能或误删

* @author toto

* @date 2017-1-12

* @note begin modify by 涂作权  2017-1-12  原始创建

*/

public void batchDel(String... pattern) throws Exception {

for (String pt : pattern) {

redisTemplate.delete(redisTemplate.keys(pt + "*"));

}

}

 

/**

* 取得缓存(int型)

* @param key

* @return

* @throws Exception

* @attention 方法的使用注意事项

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12 原始创建

*/

public Integer getInt(String key) throws Exception {

String value = (String) redisTemplate.boundValueOps(key).get();

if (StringUtils.isNotBlank(value)) {

return Integer.valueOf(value);

}

return null;

}

 

/**

* 取得缓存(字符串类型)

* @param key

* @return

* @attention 方法的使用注意事项

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12  原始创建

*/

public String getStr(String key) throws Exception {

return (String) redisTemplate.boundValueOps(key).get();

}

 

/**

* 取得缓存(字符串类型)

* @param key

* @param retain

* @return

* @attention

* @author toto

* @date 2017-1-12

* @note  begin modify by 涂作权 2017-1-12 原始创建

*/

public String getStr(String key,boolean retain) throws Exception {

String value = (String) redisTemplate.boundValueOps(key).get();

if (!retain) {

redisTemplate.delete(key);

}

return value;

}

 

/**

* 获取缓存<br>

* 注:基本数据类型(Character除外),请直接使用get(String key, Class<T> clazz)取值

* @param key

* @return

*/

public Object getObj(String key) throws Exception {

return redisTemplate.boundValueOps(key).get();

}

 

/**

* 获取缓存<br>

* @param key

* @param retain 是否保留

* @return

* @attention 注:java 8种基本类型的数据请直接使用get(String key, Class<T> clazz)取值

* @author toto

* @date 2017-1-12

* @note begin modify by 涂作权 2017-1-12  原始创建

*/

public Object getObj(String key, boolean retain) throws Exception {

Object obj = redisTemplate.boundValueOps(key).get();

if (!retain) {

redisTemplate.delete(key);

}

return obj;

}

 

/**

* 获取缓存<br>

* 注:该方法暂不支持Character数据类型

* @param key   key

* @param clazz 类型

* @return

*/

public <T> T get(String key, Class<T> clazz) throws Exception {

return (T) redisTemplate.boundValueOps(key).get();

}

 

/**

* 将value对象写入缓存

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void set(String key,Object value,long expirationtime)throws Exception {

if(value.getClass().equals(String.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Integer.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Double.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Float.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Short.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Long.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else if(value.getClass().equals(Boolean.class)){

redisTemplate.opsForValue().set(key, value.toString());

}else{

redisTemplate.opsForValue().set(key, SerializeUtils.serialize(value));

}

if(expirationtime > 0){

redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS);

}

}

 

/**

* 递减操作

* @param key

* @param by

* @return

*/

public double decr(String key, double by)throws Exception {

return redisTemplate.opsForValue().increment(key, -by);

}

 

/**

* 递增操作

* @param key

* @param by

* @return

*/

public double incr(String key, double by)throws Exception {

return redisTemplate.opsForValue().increment(key, by);

}

 

/**

* 获取double类型值

* @param key

* @return

*/

public double getDouble(String key) throws Exception {

String value = (String) redisTemplate.boundValueOps(key).get();

if(StringUtils.isNotBlank(value)){

return Double.valueOf(value);

}

return 0d;

}

 

/**

* 设置double类型值

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void setDouble(String key, double value,long expirationtime) throws Exception {

redisTemplate.opsForValue().set(key, String.valueOf(value));

if(expirationtime > 0){

redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS);

}

}

 

/**

* 设置double类型值

* @param key

* @param value

* @param time 失效时间(秒)

*/

public void setInt(String key, int value, long expirationtime) throws Exception {

redisTemplate.opsForValue().set(key, String.valueOf(value));

if(expirationtime > 0){

redisTemplate.expire(key,expirationtime, TimeUnit.SECONDS);

}

}

 

/**

* 将map写入缓存

* @param key

* @param map

* @param time 失效时间(秒)

*/

public <T> void setMap(String key, Map<String, T> map, long expirationtime)throws Exception {

redisTemplate.opsForHash().putAll(key, map);

}

 

/**

* 向key对应的map中添加缓存对象

* @param key

* @param map

*/

public <T> void addMap(String key, Map<String, T> map)throws Exception {

redisTemplate.opsForHash().putAll(key, map);

}

 

/**

* 向key对应的map中添加缓存对象

* @param key   cache对象key

* @param field map对应的key

* @param value     值

*/

public void addMap(String key, String field, String value)throws Exception {

redisTemplate.opsForHash().put(key, field, value);

}

 

/**

* 向key对应的map中添加缓存对象

* @param key   cache对象key

* @param field map对应的key

* @param obj   对象

*/

public <T> void addMap(String key, String field, T obj)throws Exception {

redisTemplate.opsForHash().put(key, field, obj);

}

 

/**

* 获取map缓存

* @param key

* @param clazz

* @return

*/

public <T> Map<String, T> mget(String key, Class<T> clazz)throws Exception {

BoundHashOperations<Serializable, String, T> boundHashOperations = redisTemplate.boundHashOps(key);

return boundHashOperations.entries();

}

 

/**

* 获取map缓存中的某个对象

* @param key

* @param field

* @param clazz

* @return

*/

public <T> T getMapField(String key, String field, Class<T> clazz)throws Exception {

return (T)redisTemplate.boundHashOps(key).get(field);

}

 

/**

* 删除map中的某个对象

* @author lh

* @date 2016年8月10日

* @param key   map对应的key

* @param field map中该对象的key

*/

public void delMapField(String key, String... field)throws Exception {

BoundHashOperations<Serializable, String, Object> boundHashOperations = redisTemplate.boundHashOps(key);

boundHashOperations.delete(field);

}

 

/**

* 指定缓存的失效时间

*

* @author FangJun

* @date 2016年8月14日

* @param key 缓存KEY

* @param time 失效时间(秒)

*/

public void expire(String key, long expirationtime) throws Exception {

if(expirationtime > 0){

redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS);

}

}

 

/**

* 添加set

* @param key

* @param value

*/

public void sadd(String key, String... value) throws Exception {

redisTemplate.boundSetOps(key).add(value);

}

 

/**

* 删除set集合中的对象

* @param key

* @param value

*/

public void srem(String key, String... value) throws Exception {

redisTemplate.boundSetOps(key).remove(value);

}

 

/**

* set重命名

* @param oldkey

* @param newkey

*/

public void srename(String oldkey, String newkey)throws Exception {

redisTemplate.boundSetOps(oldkey).rename(newkey);

}

 

/**

* 模糊查询keys

* @param pattern

* @return

*/

public Set<Serializable> keys(String pattern)throws Exception {

return redisTemplate.keys(pattern);

}

}

依赖的SerializeUtils.java代码如下:

package xx.tpl.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * SerializeUtils.java 序列化相关的操作类 * @attention * @author toto * @date 2017-1-12 * @note begin modify by 涂作权 2017-1-12 原始创建 */ public class SerializeUtils { /** * 将对象进行序列化 * @param object * @return * @attention 传递对象,将 * @author toto * @date 2017-1-12 * @note begin modify by 修改人 修改时间 修改内容摘要说明 */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } /** * 反序列化 * @param bytes * @return * @attention 反序列化的操作 * @author toto * @date 2017-1-12 * @note begin modify by 涂作权 2017-1-12 原始创建 */ public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }

 

在BaseController 中引入如下的内容:

/** 获取redis的service连接 */

@Resource(name = "redisService")

protected RedisServiceImpl redisService;

最后,在项目中就可以通过redisService调用其它的redis方法了。

原文链接:https://yq.aliyun.com/articles/603316
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章