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

一.redis安装:

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

(这里也可以用于安装redis-4.0.9.tar.gz :     wget http://download.redis.io/releases/redis-4.0.9.tar.gz)

用源码工程来编译安装

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

2、  cd /usr/local  

3、  mkdir 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>

 

设置timeout中的值为120

bin/redis-server redis.conf
auth password

bin/redis-cli -h 192.168.106.103
info clients

client list 查看客户端连接情况


config get timeout
config set timeout 600

 

二.配置Maven依赖

        <dependency>

          <groupId>org.springframework.datagroupId>

          <artifactId>spring-data-redisartifactId>

          <version>1.6.0.RELEASEversion>

      dependency>

      <dependency>

          <groupId>redis.clientsgroupId>

          <artifactId>jedisartifactId>

          <version>2.7.3version>

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

   

   

  

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

   

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

   "redis-context.xml"/>

beans>

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

 




 

 

 

 

 

编写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 {

   

         /**

     * 将内容保存到缓存中

     *

     * @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

          * @param key

          * @param elementType

          * @return

          * @throws Exception

          * @attention

          * @author toto

          * @date 2017-1-12

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

          */

         public T getByKey(final String key, Class 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;

        

         /**

          * 批量删除

          * @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;

        

         /**

          * 获取缓存

          * @param key

          * @return

          * @throws Exception

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

          * @author toto

          * @date 2017-1-12

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

          */

         public Object getObj(String key) throws Exception;

        

         /**

          * 获取缓存
 

          * @param key

          * @param retain 是否保留

          * @return

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

          * @author toto

          * @date 2017-1-12

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

          */

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

        

         /**

          * 获取缓存

          * @param

          * @return

          * @throws Exception

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

          * @author toto

          * @date 2017-1-12

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

          */

         public T get(String key, Class 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 void setMap(String key, Map map, long expirationtime)throws Exception;

   

    /**

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

     * @param key

     * @param map

     */

    public void addMap(String key, Map 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 void addMap(String key, String field, T obj)throws Exception;

   

    /**

     * 获取map缓存

     * @param key

     * @param clazz

     * @return

     */ 

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

   

    /**

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

     * @param key

     * @param field

     * @param clazz

     * @return

     */

    public T getMapField(String key, String field, Class 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 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 implements RedisService {

         @SuppressWarnings("unused")

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

         private static RedisTemplate redisTemplate;

         private long expirationtime;

        

         public long getExpirationtime() {

                   return expirationtime;

         }

 

         public RedisTemplate getRedisTemplate() {

                   return redisTemplate;

         }

 

         @SuppressWarnings("static-access")

         public void setRedisTemplate(

                            RedisTemplate 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() {

 

                            public Object doInRedis(RedisConnection connection)

                                               throws DataAccessException {

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

                                     return null;

                            }

                   });

         }

        

         /**

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

          */

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

                   try {

                            return redisTemplate.execute(new RedisCallback() {

                                     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));

                            }

                   }

         }

        

         /**

          * 批量删除

          * @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;

         }

 

         /**

          * 获取缓存

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

          * @param key

          * @return

          */

         public Object getObj(String key) throws Exception {

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

         }

 

         /**

          * 获取缓存
 

          * @param key

          * @param retain 是否保留

          * @return

          * @attention 注:java 8种基本类型的数据请直接使用get(String key, Class 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;

         }

        

         /**

     * 获取缓存

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

     * @param key   key

     * @param clazz 类型

     * @return

     */

         public T get(String key, Class 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 void setMap(String key, Map map, long expirationtime)throws Exception {

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

    }

   

    /**

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

     * @param key

     * @param map

     */

    public void addMap(String key, Map 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 void addMap(String key, String field, T obj)throws Exception {

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

    }

   

    /**

     * 获取map缓存

     * @param key

     * @param clazz

     * @return

     */

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

        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);

        return boundHashOperations.entries();

    } 

  

    /**

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

     * @param key

     * @param field

     * @param clazz

     * @return

     */

    public T getMapField(String key, String field, Class 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 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 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 中引入如下的内容:

/** 获取redisservice连接 */

@Resource(name = "redisService")

protected RedisServiceImpl redisService;

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

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部