关于redis统一session的问题

今天闹了一个大笑话,做集群后面放了一个redis用来做session统一,以前安卓,ios放在本地的验证码不能用了,故准备放到redis里做统一管理。但是我将key设置为jsessionid与session里的jsessionid相同导致报错。很尴尬


下面上redis的代码,作为保存


package com.rsxxjs.util.session;

import com.rsxxjs.util.common.ConfigUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Title			RedisClient
 * @Description 	redis 
 * @date 			2017年8月9日 
 */
public class RedisClient {
	
	public  static  JedisPool jedisPool; // 池化管理jedis链接池
	static {
		//读取相关的配置
		int maxActive = 1000;
		int maxIdle = 20;
		int maxWait = 3000;
		String ip =ConfigUtil.getProperty("redis.ip");
		int port = 6379;
		
		JedisPoolConfig config = new JedisPoolConfig();  
		//设置最大连接数
		config.setMaxTotal(maxActive);
		//设置最大空闲数
		config.setMaxIdle(maxIdle);
		//设置超时时间
		config.setMaxWaitMillis(maxWait);
		
		//初始化连接池
		jedisPool = new JedisPool(config, ip, port); 
	}
	
	
	
	/**
	 * 向缓存中设置字符串内容
	 * @param key key
	 * @param value value
	 * @return
	 * @throws Exception
	 */
	public static boolean  set(String key,String value) throws Exception{
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(jedis!=null){
				jedis.close();
			}
		}
	}
	
	
	/**
	 * 删除缓存中得对象,根据key
	 * @param key
	 * @return
	 */
	public static boolean del(String key){
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(key);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(jedis!=null){
				jedis.close();
			}
		}
	}
	
	/**
	 * 根据key 获取内容
	 * @param key
	 * @return
	 */
	public static Object get(String key){
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Object value = jedis.get(key);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(jedis!=null){
				jedis.close();
			}
		}
	}
}


下面是configutils用来读取配置文件

package com.rsxxjs.util.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

/**
 *@description		 读取配置文件工具类
 */

public class ConfigUtil
{
	 @SuppressWarnings("rawtypes")
	private static HashMap messages = new HashMap();
	 private static Properties props = new Properties();
	 
	/**
	 * @param args
	 */
	static  
	{
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        
		InputStream is = null;
        is = classLoader.getResourceAsStream("properties/common-config.properties");
        if (is != null) 
        {
            try
			{
				props.load(is);
			}
			catch (IOException e)
			{				
				e.printStackTrace();
			}
			finally
			{
				try
				{
					is.close();
				}
				catch (IOException e)
				{					
					e.printStackTrace();
				}
			}
        }
        
	}
	
	@SuppressWarnings("unchecked")
	public static String getProperty(String key)
	{
		String msg = (String)messages.get(key);
		if (msg == null)
		{
			String pros = props.getProperty(key);
			if( pros != null)
			{
				messages.put(key,pros);
			}
						
			msg = pros;
		}
		
		return msg;
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值