• jedis单机版应用


    1.pom文件添加依赖:

     2.创建配置文件

    创建单机版redisClient

     代码:

    package com.skymall.rest.dao.imp;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.skymall.rest.dao.JedisClient;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    /**
     * jedis单机版客户端 dao
    * @ClassName: JedisClientSingle 
    * @Description: TODO
    * @author   
    * @date 2018年3月22日 下午1:46:20 
    * @version V1.0
     */
    public class JedisClientSingle implements JedisClient {
    
    	@Autowired
    	private JedisPool jedisPool;
    	
    	
    	@Override
    	public String get(String key) {
    		Jedis jedis = jedisPool.getResource();
    		String value = jedis.get(key);
    		jedis.close();
    		return value;
    	}
    
    	@Override
    	public String set(String key, String value) {
    		Jedis jedis=jedisPool.getResource();
    		jedis.set(key, value);
    		jedis.close();
    		return null;
    	}
    
    	@Override
    	public String hget(String hkey, String key) {
    		Jedis jedis= jedisPool.getResource();
    		String str=jedis.hget(hkey, key);
    		jedis.close();
    		return str;
    	}
    
    	@Override
    	public long hset(String hkey, String key, String value) {
    		Jedis jedis= jedisPool.getResource();
    		long result=jedis.hset(hkey, 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.skymall.rest.jedis;
    
    import java.util.HashSet;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPool;
    
    public class TestJedis {
    
    	//测试jedis
    //	@Test
    //	public void testJedis(){
    //		//创建jedis对象
    //		Jedis jedis=new Jedis("192.168.203.137",6379);
    //		//与reids指令操作一至
    //		jedis.set("key1","88888888888888" );
    //		//删除
    //		Long del = jedis.del("cccc");
    //		//添加
    ////		String result=jedis.get("cccc");
    //		System.err.println(del);
    //		//关闭jedis对象
    //		jedis.close();
    //	}
    	
    	// 测试jedis连接池
    //	@Test
    //	public void testJedisPool(){
    //		//创建连接池
    //		JedisPool jedisPool=new JedisPool("192.168.203.137",6379);
    //		//从连接池里取jedis对象
    //		Jedis jedis = jedisPool.getResource();
    //		//一下操作都一样
    //		String result=jedis.get("ddd");
    //		System.err.println(result);
    //	    //关闭jedis
    //		jedis.close();
    //		//关闭连接池
    //		jedisPool.close();
    //	}
    //	
    //	
    //	//测试redis集群(自带连接池)不需要关闭否则会报错
    //	@Test
    //	public void testJedisCluster(){
    //		HashSet<HostAndPort> nodes=new HashSet<>();
    //		nodes.add(new HostAndPort("192.168.203.137", 6001));
    //		nodes.add(new HostAndPort("192.168.203.137", 6002));
    //		nodes.add(new HostAndPort("192.168.203.137", 6003));
    //		nodes.add(new HostAndPort("192.168.203.137", 6004));
    //		nodes.add(new HostAndPort("192.168.203.137", 6005));
    //		nodes.add(new HostAndPort("192.168.203.137", 6006));
    //		JedisCluster cluster=new JedisCluster(nodes);
    //		cluster.set("key2","成功了");
    //		System.out.println(cluster.get("key2"));
    //	
    //		
    //	}
    //	//测试单机版jedis与spring整合
    //	@Test
    //	public void testJedisAndSpring(){
    //		
    //		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
    //		JedisPool jedisPool=(JedisPool) applicationContext.getBean("redisClient");
    //		Jedis jedis=jedisPool.getResource();
    //		jedis.set("gggg", "09090900");
    //		String str=jedis.get("gggg");
    //		System.out.println(str);
    //		
    //		jedis.close();
    //		jedisPool.close();
    //	}
    //	
    //	//测试jedis集群与spring整合
    //	@Test
    //	public void JedisClusterAndSpring(){
    //		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
    //		JedisCluster jedisCluster=(JedisCluster) applicationContext.getBean("redisClient");
    //		jedisCluster.set("name","8822288");
    //		String str=jedisCluster.get("name");
    //		System.out.println(str);
    //	
    //		
    //	}
    }
    

      

  • 相关阅读:
    lodash kebabCase
    lodash escapeRegExp 转义正则特殊字符
    lodash capitalize 首字母大写
    lodash camelCase 驼峰写法
    lodash pick
    lodash random
    lodash round
    Linux 目录结构
    每天一个linux命令(6/18):lsof命令
    Linux 内核编译步骤及配置详解
  • 原文地址:https://www.cnblogs.com/shianliang/p/9277650.html
Copyright © 2020-2023  润新知