Redis java开发

一、环境准备

   1.部署安装redis:https://blog.csdn.net/weixin_41794654/article/details/103835193

   2.权限等配置: redis.conf配置文件

          设置外网可以访问redis:   bind 0.0.0.0      代表不限制访问IP   

          设置访问密码:      requirepass 123456   代表设置密码为123456

3.重新启动redis

          注意:此时启动redis需要指定配置文件,否则配置文件修改内容不会生效。

二、普通java连接redis

1.引入相关依赖


	redis.clients
	jedis
	2.7.1


	org.apache.commons
	commons-lang3
	3.3.2

2.编写程序

public class SimpleRedisTest {

    //服务器IP地址
    private static String ADDR = "*.*.*.*";
    //端口
    private static int PORT = 6379;
    //密码
    private static String AUTH = "123456";
    //连接实例的最大连接数
    private static int MAX_ACTIVE = 20;
    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    private static int MAX_IDLE = 2;
    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
    private static int MAX_WAIT = 10000;
    //连接超时的时间  
    private static int TIMEOUT = 10000;
    // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;
    //数据库模式是16个数据库 0~15
    public static final int DEFAULT_DATABASE = 0;
    /**
     * 初始化Redis连接池
     */

    static {

        try {

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            //jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH,DEFAULT_DATABASE);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH,DEFAULT_DATABASE);
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    /**
     * 获取Jedis实例
     */

    public synchronized static Jedis getJedis() {

        try {

            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                System.out.println("redis--服务正在运行: "+resource.ping());
                return resource;
            } else {
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /***
     *
     * 释放资源
     */

    public static void returnResource(final Jedis jedis) {
        if(jedis != null) {
            jedisPool.returnResource(jedis);
        }

    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        Boolean exists = jedis.exists("hello");
        System.out.println("是否存在key: "+exists);
    }

}

三、springboot web环境使用redis

1.依赖信息



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
	

	com.demo.practice
	practice-demo
	0.0.1-SNAPSHOT
	practice-demo
	Demo project for Spring Boot

	
		1.8
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-test
		
		
			junit
			junit
		

		
		
			org.springframework.boot
			spring-boot-starter-data-redis
		

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


2.编写程序

@RunWith(SpringRunner.class)
@SpringBootTest(classes = PracticeDemoApplication.class)// 与上文启动类-极简版对应
public class WebRedisTest {

    //引入redisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedis(){
        String key = "test";
        String value = "测试";
        //插入缓存
        redisTemplate.opsForValue().set(key, value);
        //取缓存
        System.out.println(redisTemplate.opsForValue().get("test"));
    }

}

 

 

 

 

 

 

 

你可能感兴趣的:(TECH)