您的位置:首页 > 数据库 > Redis

关于redis统一session的问题

2017-08-14 09:00 162 查看
今天闹了一个大笑话,做集群后面放了一个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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: