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

简介: redis安装,redis项目以来,redis和spring整合,redis的service,redis的service实现类 一.redis安装: Redis-3.2.6.tar.gz安装,参考方式: 用源码工程来编译安装 1、  到官网下载最新stable版,这里使用的是:redis-3.

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方法了。

相关文章
|
8月前
|
关系型数据库 应用服务中间件 nginx
Docker一键安装中间件(RocketMq、Nginx、MySql、Minio、Jenkins、Redis)
本系列脚本提供RocketMQ、Nginx、MySQL、MinIO、Jenkins和Redis的Docker一键安装与配置方案,适用于快速部署微服务基础环境。
|
5月前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
697 6
|
8月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
829 3
|
8月前
|
存储 缓存 NoSQL
Redis 核心知识与项目实践解析
本文围绕 Redis 展开,涵盖其在项目中的应用(热点数据缓存、存储业务数据、实现分布式锁)、基础数据类型(string 等 5 种)、持久化策略(RDB、AOF 及混合持久化)、过期策略(惰性 + 定期删除)、淘汰策略(8 种分类)。 还介绍了集群方案(主从复制、哨兵、Cluster 分片)及主从同步机制,分片集群数据存储的哈希槽算法。对比了 Redis 与 Memcached 的区别,说明了内存用完的情况及与 MySQL 数据一致性的保证方案。 此外,详解了缓存穿透、击穿、雪崩的概念及解决办法,如何保证 Redis 中是热点数据,Redis 分布式锁的实现及问题解决,以及项目中分布式锁
241 1
|
8月前
|
前端开发 Java API
酒店管理系统基于 JavaFX Spring Boot 和 React 经典项目重构实操
本文介绍了基于现代技术栈的酒店管理系统开发方案,整合了JavaFX、Spring Boot和React三大技术框架。系统采用前后端分离架构,JavaFX构建桌面客户端,React开发Web管理界面,Spring Boot提供RESTful API后端服务。核心功能模块包括客房管理和客户预订流程,文中提供了JavaFX实现的客房管理界面代码示例和React开发的预订组件代码,展示了如何实现客房信息展示、添加修改操作以及在线预订功能。
556 0
|
8月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
327 0
|
8月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
658 0
|
8月前
|
Java 应用服务中间件 Maven
第01课:Spring Boot开发环境搭建和项目启动
第01课:Spring Boot开发环境搭建和项目启动
2178 0
|
10月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
1006 16

热门文章

最新文章