RedisConnection.java 3.1 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
package org.maxkey.persistence.redis;

import java.io.Serializable;
import java.util.List;

import org.maxkey.util.ObjectTransformer;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class RedisConnection {

	Jedis conn ;
	RedisConnectionFactory connectionFactory;
	
	Pipeline pipeline ;
	 
	public RedisConnection() {
		
	}
	
	public RedisConnection(RedisConnectionFactory connectionFactory) {
		this.conn=connectionFactory.open();
		this.connectionFactory=connectionFactory;
	}

	/**
	 * @param key
	 * @param value
	 */
	public  void set(String key, String value){
		conn.set(key, value);
	}
	

	/**
	 * @param key
	 * @param value
	 */
	public  void setObject(String key, Serializable object){
		set(key, ObjectTransformer.serialize(object));
	}
	
	public  void setexObject(String key,int seconds, Serializable object){
		setex(key, seconds, ObjectTransformer.serialize(object));
	}
	
	/**
	 * @param key
	 * @param seconds
	 * @param value
	 */
MaxKey单点登录官方's avatar
v3.0.0  
MaxKey单点登录官方 已提交
70
	public  void setex(String key,long seconds, String value){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		if(seconds==0){
			conn.setex(key, RedisConnectionFactory.DEFAULT_CONFIG.DEFAULT_LIFETIME, value);
		}else{
			conn.setex(key, seconds, value);
		}
	}
	
	
	/**
	 * @param key
	 * @return String 
	 */
	public  String get(String key){
		String value = null;
		if(key != null){
			value = conn.get(key);
		}
		return value;
	}
	
	/**
	 * @param key
	 * @return String 
	 */
	public  <T> T getObject(String key){
		String value = null;
		if(key != null){
			value = get(key);
			if(value!=null){
				return ObjectTransformer.deserialize(value);
			}
		}
		return null;
	}
	
MaxKey单点登录官方's avatar
v3.0.0  
MaxKey单点登录官方 已提交
106
	public void expire(String key,long seconds){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		conn.expire(key, seconds);
	}
	
	public void delete(String key){
		conn.del(key);
	}
	
	public  void rPush(String key, Serializable object){
		conn.rpush(key, ObjectTransformer.serialize(object));
	}
	public long  lRem(String key,int count,String value){
		return conn.lrem(key, count, value);
	}
	
	
	public List<String>  lRange(String key,int start,int end){
		return conn.lrange(key, start, end);
	}
	
	public void openPipeline(){
		this.pipeline=conn.pipelined();
	}
	
	public List<Object> closePipeline(){
		return pipeline.syncAndReturnAll();
	}
	/**
     * 释放jedis资源
     * @param jedis
     */
	public  void close() {
        if (conn != null) {
        	connectionFactory.close(conn);
        }
    }

	public Jedis getConn() {
		return conn;
	}

	public void setConn(Jedis conn) {
		this.conn = conn;
	}
W
wzy 已提交
150 151 152 153 154

	public Pipeline getPipeline() {
		return pipeline;
	}
	
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
155 156
	
}