• jedisClient操作redis实现增删改查功能


    这个集群环境下和单机环境下:

    package com.taotao.sso.dao.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.taotao.sso.dao.JedisClient;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    //单机版的dao实现类
    public class JedisClientSingle implements JedisClient{
    	@Autowired
    	private JedisPool jedisPool;
    
    	@Override
    	public String get(String key) {
    		//连接池先要获取资源空间,然后再设值和获取值
    		Jedis jedis=jedisPool.getResource();
    		String string=jedis.get(key);
    		jedis.close();
    		return string;
    	}
    
    	@Override
    	public String set(String key, String value) {
    		Jedis jedis=jedisPool.getResource();
    		String string=jedis.set(key, value);
    	     jedis.close();
    		return string;
    	}
    
    	@Override
    	public String hget(String hashKey, String key) {
    		Jedis jedis=jedisPool.getResource();
    		String string=jedis.hget(hashKey, key);
    		jedis.close();
    		return string;
    	}
    
    	@Override
    	public Long hset(String hashKey, String key, String value) {
    		Jedis jedis=jedisPool.getResource();
    		Long result=jedis.hset(hashKey,key, value);
    		jedis.close();
    		
    		return result;
    	}
    
    	@Override
    	public long incr(String key) {
    		Jedis jedis = jedisPool.getResource();
    		Long result = jedis.incr(key);
    		jedis.close();
    
    		return result;
    	}
    
    	@Override
    	public long expire(String key, int second) {
    		Jedis jedis = jedisPool.getResource();
    		Long result = jedis.expire(key, second);
    		jedis.close();
    		return result;
    
    	}
    //设置时间期限
    	@Override
    	public long ttl(String key) {
    		Jedis jedis = jedisPool.getResource();
    		Long result = jedis.ttl(key);
    		jedis.close();
    		return result;
    
    		
    	}
    
    	@Override
    	public long del(String key) {
    		Jedis jedis = jedisPool.getResource();
    		Long result = jedis.del(key);
    		jedis.close();
    		return result;
    	}
    
    	@Override
    	public long hdel(String hkey, String key) {
    		Jedis jedis = jedisPool.getResource();
    		Long result = jedis.hdel(hkey, key);
    		jedis.close();
    		return result;
    	}
    	
    }
    

      集群下:

    package com.taotao.sso.dao.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.taotao.sso.dao.JedisClient;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPool;
    //集群版的dao实现类
    public class JedisClientCluster implements JedisClient{
    	@Autowired
    	private JedisCluster  jedisCluster;
    
    	@Override
    	public String get(String key) {
    		
    		return jedisCluster.get(key);
    	}
    
    	@Override
    	public String set(String key, String value) {
    	
    		return jedisCluster.set(key, value);
    	}
    
    	@Override
    	public String hget(String hkey, String key) {
    		
    		return jedisCluster.hget(hkey, key);
    	}
    
    	@Override
    	public Long hset(String hkey, String key, String value) {
    		return jedisCluster.hset(hkey, key, value);
    	}
    
    	@Override
    	public long incr(String key) {
    		return jedisCluster.incr(key);
    	}
    
    	@Override
    	public long expire(String key, int second) {
    		return jedisCluster.expire(key, second);
    	}
    
    	@Override
    	public long ttl(String key) {
    		
    		return jedisCluster.ttl(key);
    	}
    //若内容修改后,更新缓存中的内容,将之前在缓存中的key删除
    	@Override
    	public long del(String key) {
    		
    		return jedisCluster.del(key);
    	}
    
    	@Override
    	public long hdel(String hkey, String key) {
    		
    		return jedisCluster.hdel(hkey, key);
    	}
    
    	
    }
    

      

  • 相关阅读:
    JavaScript 相等(==)与全等(===)操作符
    JavaScript 判断空对象、空数组的方法
    JavaScript中的深拷贝与浅拷贝
    JS trim去除字符串收尾指定字符
    Django+Markdown+Pygments 支持Markdown 实现代码高亮
    crontab 定时服务
    程序员如何修复婚姻的bug
    向Mysql 中插入汉字(Emoji)出现 Incorrect string value
    根据html页面id寻找对应的Js文件
    Django Pagination
  • 原文地址:https://www.cnblogs.com/fengli9998/p/6403177.html
Copyright © 2020-2023  润新知