Redis实现中间件

        Redis基本教程: http://www.runoob.com/redis/redis-tutorial.html

        其实Redis很简单,就是充当缓存的作用, 不能替代MySQL(及其它)数据库。 做个比喻: 数据库就相当于硬盘,Redis就相当于内存, 在Redis里读写数据更快。 Redis一般作为中间件,服务将数据缓存到Redis里, 但必须要注意跟数据库的同步问题!!! 

       原则:先在Redis里查,如果查不到再去数据库查, 并保持结果到Redis里。

       因为Redis使用关键字读写键值的, 如果多个团队使用同一个Redis服务, 那么很可能出现关键字冲突的问题。 解决办法有2个:

1、 为每个团队分配不同的关键字前缀, 例如 jkcom_user***,jkcom_sell***等。

2、 每个团队使用不同的database, 同一个database要保证key不同,不同database的key可以相同。 (PS

: 每个database都是独立的存储空间,  在redis.conf配置,默认有16个,即SELECT 0~SELECT 15。)

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

        在redis目录里执行./src/redis-server启动redis服务, 默认使用redis.conf配置文件。


     执行./src/redis-cli连接到客户端。




API说明:http://docs.spring.io/spring-data/redis/docs/current/api/

Maven包最新代码地址:

spring-data-redis:  https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
jedis:  https://mvnrepository.com/artifact/redis.clients/jedis


在SpringMVC的配置文件里添加redis的代码包, 版本号去文章上面仓库去查:



     创建redis的配置文件, Java代码会读取这个文件。 PS:在实际开发中, 要写多个配置文件分别对应开发、测试、预上线、线上环境等(通过配置springmvc的profiles标签切换)。


     这里有个坑: 如果要设置database, 必须先设置密码!!!

     下面代码可以拿到Jedis实例引用, 然后就可以调用它的增删改查方法了。
public class RedisProvider {

    protected static JedisPool jedispool;

    static{
        ResourceBundle bundle = ResourceBundle.getBundle("redis");  //读取redis.properties文件
        if (bundle == null) {
            throw new IllegalArgumentException(
                    "[redis.properties] is not found!");
        }
        try {
            JedisPoolConfig jedisconfig = new JedisPoolConfig();
            jedisconfig.setMaxIdle(Integer.valueOf(bundle
                    .getString("redis.pool.maxIdle")));
            jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle
                    .getString("redis.pool.testOnBorrow")));
            jedisconfig.setTestOnReturn(Boolean.valueOf(bundle
                    .getString("redis.pool.testOnReturn")));
            jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),
                    Integer.valueOf(bundle.getString("redis.port")),
                    Integer.valueOf(bundle.getString("redis.timeout")),
                    bundle.getString("redis.password"),
                    Integer.valueOf(bundle.getString("redis.database")));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Jedis getJedis() {
        Jedis jedis = null;
        try {
            jedis = jedispool.getResource();
        } catch (JedisConnectionException jce) {
            jce.printStackTrace();
        }
        return jedis;
    }


}

   编写最简单的读写字符串示例:



          OK, 成功!
          Redis还支持list,set,zset,hash等数据结构。
        
        实际生产环境至少部署主从redis服务器, 对于大数据量的服务要部署redis分布式集群(Codis,阿里redis集群或redis sentinel)。


  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值