Redis

NoSQL

泛指非关系型数据库:
high performance:对数据库高并发读写的需求
huge Storage:对海量数据的高效率存储和访问的需求
high Scalability &&high availability:对数据库的高可扩展性和高可用性的需求

Redis(一款NoSQL的数据库产品)

Redis是直接从内存中读数据,所以当数据量很大的时候,读写速度要比mysql快的多
Redis: 软件–C语言–存储数据
特点:key-value ,内存存储

NOSQL的数据库分类:

  1. 键值(Key-Value)存储数据库;Map
  2. 列存储数据库;
  3. 文档型数据库;
  4. 图形(Graph)数据库;
    Redis是用C语言开发的一个开源的高性能键值对(Key-Value)数据库。
    Redis支持的数据类型有:
    1. 字符串类型 string(常用:json/xml)
  5. 散列类型 hash(key–value)map
  6. 列表类型 list linkedlist 用户列表
  7. 集合类型 set
  8. 有序集合类型 sortedset
//读写redis的工具类
public class JedisUtil {
    private static JedisPool jedisPool;
    private  static  int maxtotal;
    private static  int maxwaitmillis;
    private static  String host;
    private static  int port;
    //加载配置文件
    static{
        ResourceBundle jedisPorperties = ResourceBundle.getBundle("jedis");
        maxtotal = Integer.valueOf(jedisPorperties.getString("maxtotal"));
        maxwaitmillis = Integer.valueOf(jedisPorperties.getString("maxwaitmillis"));
        port = Integer.valueOf(jedisPorperties.getString("port"));
        host = jedisPorperties.getString("host");
    }
    //初始化连接池
    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxtotal);
        config.setMaxIdle(maxwaitmillis);
        jedisPool = new JedisPool(config, host, port);
    }
    /*
    获取jedis客户端操作对象
     */
    public static Jedis getJedis(){
        return  jedisPool.getResource();
    }
    //释放资源
    public static void close(Jedis jedis){
        if(null !=jedis){
            jedis.close();
        }
    }
}
public class PoemService {
    //查询数据中所有的poem集合
    public String queryAll () throws JsonProcessingException {
        //先从redis中查。如果查到为空,那么再从数据库中查
        //计算一下redis查的时间
        long rStar = System.currentTimeMillis();
        Jedis jedis = JedisUtil.getJedis();
        String poemsStr = jedis.get("poems");
        long rend = System.currentTimeMillis();
        System.out.println("redis查找用时"+(rend-rStar));
        if (null== poemsStr){
            long SQLStar = System.currentTimeMillis();
            PoemDao poemDao = new PoemDao();
            List<Poem> poems = poemDao.queryAll();
            long SQLend = System.currentTimeMillis();
            System.out.println("mysql查找用时"+(SQLend-SQLStar));
            //将poems转换为json格式
            poemsStr = new ObjectMapper().writeValueAsString(poems);
            //将poemsStr存到reids中
            jedis.set("poems",poemsStr);
        }
        //返回poemsStr json格式
        return poemsStr;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值