java web项目配置Redis


 

java web项目配置Redis

Redis介绍

  REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。

 

安装redis

环境准备:win 7环境、reids3.0

步骤1:下载redis解压到任意盘符,结果如下:

 

步骤2:点击redis-server.exe启动redis服务;

步骤3:选择redis-cli.exe启动redis客户端,进行测试。输入set mykey redis 回车,输入 get mykey 回车,输出:“redis”。安装完成.

项目配置reids

spring项目下,需要导入commons-pool-1.5.6.jar、commons-pool2-2.4.2.jar、jedis-2.7.3.jar、spring-data-redis-1.6.0.RELEASE.jar文件。在spring配置文件中加入以下元素:

 <bean id="redisUtil" class="com.project.controller.Redis.RedisUtil">
        <!-- 初始化类 -->
        <property name="addr"><value>127.0.0.1</value></property>
        <!-- 访问地址,默认本地 -->
        <property name="port"><value>6379</value></property>
        <!-- 端口号 -->
        <property name="auth"><value>master</value></property>
        <property name="maxIdle"><value>200</value></property>
        <property name="maxActive"><value>1024</value></property>
        <property name="maxWait"><value>10000</value></property>
        <property name="timeOut"><value>10000</value></property>
        <property name="testOnBorrow"><value>true</value></property>
    </bean> 

 RedisUtil代码:

public class RedisUtil implements Serializable{

    private static final long serialVersionUID = -1149678082569464779L;

    //Redis服务器IP
    private static String addr;

    //Redis的端口号
    private static int port;

    //访问密码
    private static String auth;

    //可用连接实例的最大数目,默认值为8;
    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
    private static int maxActive;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    private static int maxIdle;

    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
    private static int maxWait;

    private static int timeOut;

    //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean testOnBorrow;

    public static Jedis jedis;//非切片额客户端连接

    public static JedisPool jedisPool;//非切片连接池

    public static ShardedJedis shardedJedis;//切片额客户端连接

    public static ShardedJedisPool shardedJedisPool;//切片连接池

    private static void initialPool() 
    { 
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive); 
        config.setMaxIdle(maxIdle); 
        config.setMaxWaitMillis(maxWait); 
        config.setTestOnBorrow(testOnBorrow);
        jedisPool = new JedisPool(config, addr, port);
    }
    private static  void initialShardedPool() 
    { 
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive); 
        config.setMaxIdle(maxIdle); 
        config.setMaxWaitMillis(maxWait); 
        config.setTestOnBorrow(testOnBorrow);
        // slave链接 
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); 
        shards.add(new JedisShardInfo(addr, port, auth)); 

        // 构造池 
        shardedJedisPool = new ShardedJedisPool(config, shards); 
    }

    public  static void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        initialPool(); 
        initialShardedPool();
        try {
              shardedJedis = shardedJedisPool.getResource(); 
        } catch (Exception e) {
            System.out.println("连接shardedJedisPool失败!");
        }
        try {
             jedis = jedisPool.getResource();
        } catch (Exception e) {
            System.out.println("连接jedisPool失败!");
        }
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public int getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }

    public int getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }

    public int getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(int maxWait) {
        this.maxWait = maxWait;
    }

    public int getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }

    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }
}

  

功能测试

 

//redis-server.exe启动状态下
public void selectReviewsByGoodsId(HttpServletRequest request){
            try {
                Jedis jedis = new Jedis();
                Date time1 = new Date();
                jedis.set("name","zhang");
                Date time2 = new Date();
                System.out.println("时间:"+(time2.getTime()-time1.getTime()));
                System.out.println("存入redis完毕");
                System.out.println(jedis.get("name"));
            } catch (Exception e) {
                //如果缓存连不上,则不处理
                System.out.println("登录无法更新该用户缓存");
            }
    }

 

 

 

这是刚学redis和项目配置的时候学的;希望可以共同进步

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM