Redis目前被广泛的运用到项目中,身为一个后台开发者,如果你不识redis或者说没有接触过相关缓存的技术,那你应该马上学习起来了。可以说这也是技术一个小分水岭了,初中级程序员写程序很大一个区别就是思想,其实也和了解的技术相关,如果都不了解谈何去思考用什么,这也就是为什么有些程序猿写出来的程序跑起来和蜗牛一样。

那Reids难学么?

个人给的答案是:如果是入门,那是简单的不行了,可以说一两天就可以大致入门了。

入门过程:1.你应该先对redis进行了解,可以去看看相关教程文档,像菜鸟就比较好。2.把它在你的电脑上装起来,运行起来玩玩,推荐个图形化工具Redis Desktop Manager 用工具连接应该会更直观。3.在程序中连接进行些基本的操作。这是本人当时入门的过程。

下面来谈谈Reids把! 在这之前应该了解了解何为缓存,因为Redis绝大部分是用来做缓存的,但也有些人用来做消息队列。个人不推荐。

什么是缓存呢?

缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找。由于缓存的运行速度比内存快得多,故缓存的作用就是帮助硬件更快地运行。缓存往往使用的是RAM(断电即掉的非永久储存),电脑里最大的缓存就是内存条了。

那为什么我们的程式要用到缓存?

减轻数据库服务器压力,这应该大家都是了解的,程序在进行数据库访问的时候是比较慢的(相对来说),如果一个应用程序用户访问一次,就要去数据库访问一次,拿些数据出来呈现,那某一时刻多人访问,你的数据库服务器就可能死。个人玩mysql的经历,以前在一个阿里云服务器上(便宜货,低配)装了个mysql,然后跑了自己写的程序,连接池大概设了300,数据是异步处理然后塞到数据库,大概跑个400/s的并发,持续2分钟。当然接口响应是没问题,数据进来也有做缓存,然后插入到数据库。就是对数据库插入数据的时候有瓶颈。一跑起来,mysql一下就死了(运行大概在10s后就会死了)。所以说减轻数据库压力其实对于一个程序来说是很重要的,我们将一些数据缓存起来,程序首先去查看缓存中有没有,若没有再去访问数据库。

不知道有用mysql的,有没有去看看mysql的安装目录,有没有去看看我们存入mysql中的数据存储在哪里。

Redis编程容易吗 redis难学吗_nosql

点进去可以发现我的数据都存在里面,以.frm,.ibd,.opt文件存储,那明显是存储在安装的硬盘上。

为什么使用Redis做缓存?

   1.Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
   2.Redis支持master-slave(主-从)模式应用
   3.Redis支持数据持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
   4.Redis单个value的最大限制是1GB,memcached只能保存1MB的数据。

下面实现一个简单的连接redis操作.   提醒:springboot不要使用2.0(包括2.0)以上版本,不然配置需要改变

目录结构

Redis编程容易吗 redis难学吗_springboot redis_02

依赖

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

application.properties

#redis jedis配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
#spring-session 使用
spring.session.store-type=none

redis配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * SpringSession  需要注意的就是redis需要2.8以上版本,然后开启事件通知,在redis配置文件里面加上
     * notify-keyspace-events Ex
     * Keyspace notifications功能默认是关闭的(默认地,Keyspace 时间通知功能是禁用的,因为它或多或少会使用一些CPU的资源)。
     * 或是使用如下命令:
     * redis-cli config set notify-keyspace-events Egx
     * 如果你的Redis不是你自己维护的,比如你是使用阿里云的Redis数据库,你不能够更改它的配置,那么可以使用如下方法:在applicationContext.xml中配置
     * <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
     * @return
     */

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.pool.min-idle}")
    private int minIdle;

    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;

    @Bean
    public JedisPool redisPoolFactory(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,null);

        return  jedisPool;
    }

}
JedisClient
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public interface JedisClient {

    String get(String key);

    String set(String key, String value);

    String hget(String hkey, String key);

    long hset(String hkey, String key, String value);

    long incr(String key);

    long expire(String key, int second);

    long ttl(String key);

    long del(String key);

    long hdel(String hkey, String key);

    boolean exists(String key);

    long lpush(String key, String value);

    String rpop(String key);

    List<String> brpop(int timeout, String key);


}
JedisClientSingle
import com.redis.service.JedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;

@Service
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 hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @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;
    }

    @Override
    public boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    @Override
    public long lpush(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.lpush(key, value);
        jedis.close();
        return result;
    }

    @Override
    public String rpop(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.rpop(key);
        jedis.close();
        return result;
    }

    @Override
    public List<String> brpop(int timeout, String key) {
        Jedis jedis = jedisPool.getResource();
        List<String> result = jedis.brpop(timeout, key);
        jedis.close();
        return result;
    }
MyApplicationRunner
import com.redis.service.JedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Auther: Administrator
 * @Date: 2018/9/15 0:18
 * @Description:
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    private JedisClient jedisClient;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        jedisClient.set("hello","小明");
        System.out.println(jedisClient.get("hello"));
    }
}

结果

Redis编程容易吗 redis难学吗_Redis编程容易吗_03

本地redis

Redis编程容易吗 redis难学吗_nosql_04