Java对Redis基本使用

1 引入jar包

  java是通过Jedis对redis进行操作的,首先引入jedis.jar

  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
  </dependency>


2 建立redis连接池

  

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisPool {
	
	// 非切片客户端链接对象
	private Jedis jedis;
	// 非切片链接池对象
	private JedisPool jedisPool;
	// 切片客户端链接对象
	private ShardedJedis shardedJedis;
	// 切片链接池
	private ShardedJedisPool shardedJedisPool;
	
	private String ip = "127.0.0.1";
	private int port = 6379;

	public RedisPool(){
		initializePool();
		initializeShardedPool();
		setJedis(jedisPool.getResource());
		setShardedJedis(shardedJedisPool.getResource());
	}
	
	public RedisPool(String ip){
		this.ip = ip;
		initializePool();
		initializeShardedPool();
		setJedis(jedisPool.getResource());
		setShardedJedis(shardedJedisPool.getResource());
	}
	
	public RedisPool(String ip, int port){
		this.ip = ip;
		this.port = port;
		initializePool();
		initializeShardedPool();
		setJedis(jedisPool.getResource());
		setShardedJedis(shardedJedisPool.getResource());
		
	}
	
	// 初始化非切片池 
	public void initializePool(){
		//池的配置
		JedisPoolConfig jpc = new JedisPoolConfig();
		//最大空闲连接数
		jpc.setMaxIdle(20);
		jpc.setMaxIdle(5);
		//获取连接时的最大等待毫秒数  
		jpc.setMaxWaitMillis(1000);
		//在空闲时检查有效性, 默认false  
		jpc.setTestOnBorrow(false);
		jedisPool = new JedisPool(jpc, ip, port);
	}
	
	// 初始化切片池
	public void initializeShardedPool(){
		//池的配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(20);
		config.setMaxWaitMillis(1000);
		config.setTestOnBorrow(false);
		// slave链接 
		//可以实现集群的功能,配置多个redis服务实现请求的分配进行负载均衡  
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo(ip, port, "master"));
		// 构造池
		shardedJedisPool = new ShardedJedisPool(config, shards);
	}
	
	public void closeJedisPool(){
		jedisPool.close();
	}
	public void closeShardedJedisPool(){
		shardedJedisPool.close();
	}

	public Jedis getJedis() {
		return jedis;
	}

	public void setJedis(Jedis jedis) {
		this.jedis = jedis;
	}

	public ShardedJedis getShardedJedis() {
		return shardedJedis;
	}

	public void setShardedJedis(ShardedJedis shardedJedis) {
		this.shardedJedis = shardedJedis;
	}
}

  

3 定义操作接口

  

import java.util.Map;
import java.util.Set;

public interface IRedisService {

	
	public Boolean setString(String key, String value);
	public String getString(String key);
	public Boolean existsKey(String key);
	public Long delKey(String key);
	public String typeKey(String key);
	public Set<String> keys(String key);
	
	/**
	 * 获得map数据集
	 * @param key
	 * @return
	 */
	public Map<String, String> getMap(String key);
	
	/**
	 * 设置map数据集
	 * @param key
	 * @param map
	 * @return
	 */
	public Boolean setMap(String key, Map<String, String> map);
	
	/**
	 * 获得map字段中的值
	 * @param key
	 * @param fieldKey
	 * @return
	 */
	public String getMapFieldValue(String key, String fieldKey);
	
	/**
	 * 获得map中多个字段值
	 * @param key
	 * @param fieldKeys
	 * @return
	 */
	public Map<String, String> getMapFieldValues(String key, String[] fieldKeys);
	
	/**
	 * 设置map中具体的字段值
	 * 参考存储格式:{key,map{fieldKey, fieldValue}}
	 * @param key
	 * @param fieldKey
	 * @param fieldValue
	 * @return
	 */
	public Boolean setMapFieldValue(String key, String fieldKey, String fieldValue);
	
	
}

  

4 接口实现类

  

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.robert.redis.client.config.RedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;

public class RedisService implements IRedisService{

	private RedisPool redisPool;
	
	public RedisService(){
		redisPool = new RedisPool();
	}
	
	public RedisService(String host){
		redisPool = new RedisPool(host);
	}
	
	public RedisService(String host, int port){
		redisPool = new RedisPool(host, port);
	}
	
	private Jedis getJResource(){
		Jedis jResource = null;
		jResource = redisPool.getJedis();
		return jResource;
	}
	private ShardedJedis getShardResource(){
		ShardedJedis sResource = null;
		sResource = redisPool.getShardedJedis();
		return sResource;
	}
	
	public Boolean setString(String key, String value) {
		boolean result = false;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				resource.set(key, value);
				result = true;
			}
			
		}catch(Exception e){
			result = false;
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public String getString(String key) {
		String result = null;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				result = resource.get(key);
			}
			
		}catch(Exception e){
			result = null;
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public Boolean existsKey(String key) {
		Boolean result = false;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				result = resource.exists(key);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		
		return result;
	}

	public Long delKey(String key) {
		Long result = null;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				result = resource.del(key);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public String typeKey(String key) {
		String result = null;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				result = resource.type(key);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public Set<String> keys(String key) {
		Set<String> result = null;
		Jedis resource = null;
		try{
			resource = getJResource();
			if(resource != null){
				result = resource.keys(key);
			}
			
		}catch(Exception e){
			result = null;
			e.printStackTrace();
		}finally{
			redisPool.closeJedisPool();
		}
		return result;
	}

	public Map<String, String> getMap(String key) {
		Map<String, String> map = null;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null && resource.exists(key)){
				map = resource.hgetAll(key);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return map;
	}

	public Boolean setMap(String key, Map<String, String> map) {
		Boolean result = false;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				resource.hmset(key, map);
				result = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public String getMapFieldValue(String key, String fieldKey) {
		String result = null;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null && resource.hexists(key, fieldKey)){
				result = resource.hget(key, fieldKey);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

	public Map<String, String> getMapFieldValues(String key, String[] fieldKeys) {
		Map<String, String> map = new HashMap<String, String>();
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null){
				for(String fieldKey : fieldKeys){
					map.put(fieldKey, resource.hget(key, fieldKey));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return map;
	}

	public Boolean setMapFieldValue(String key, String fieldKey, String fieldValue) {
		Boolean result = false;
		ShardedJedis resource = null;
		try{
			resource = getShardResource();
			if(resource != null && resource.exists(key)){
				resource.hset(key, fieldKey, fieldValue);
				result = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redisPool.closeShardedJedisPool();
		}
		return result;
	}

}

  

5 测试

  

 public static void main( String[] args )
    {
        
    	IRedisService rs = new RedisService();
    	rs.setString("test", "Hello Redis!");
    	System.out.println(rs.getString("test"));
    }

  

posted @ 2017-11-01 17:07  M_Y_522  阅读(1092)  评论(0编辑  收藏  举报