JAVA从零学re从零开始的JAVA学习05——<javaEE阶段三基础版>

day05 Redis数据库的简单配置使用

1.非关系型数据库NOSQL

概念:随着互联网的高速崛起,网站的用户群的增加,访问量的上升,传统数据库上都开始出现了性能瓶颈,web程序

不再仅仅专注在功能上,同时也在追求性能。所以NOSQL数据库应运而上,具体表现为对如下三高问题的解决

  •  High performance - 对数据库高并发读写的需求   
  •  Huge Storage - 对海量数据的高效率存储和访问的需求 
  •  High Scalability && High Availability- 对数据库的高可扩展性和高可用性的需求

主流产品

在大数据存取上具备关系型数据库无法比拟的性能优势

 

2.Redis数据库

redis就是在内存中的一个大的map集合

Map<String,Object>

——字符串类型 String

  Map<String,String>

——散列类型 Hash

  Map<String,Map<String,String>>

——列表类型 List

  Map<String,List<String>>

——集合类型 Set

  Map<String,Set<String>>

——有序集合类型 sortedset

  Map<String,sortedset<String>>

Redis在windows的安装先在下载zip包

目录结构:

 -双击redis-server开启服务  点击redis-cli开启客户端

 在另一台电脑上链接可以进入目录redis -cli.exe -h 127.0.0.1  -p6379(redis的端口号)

 -为了不用每次手动启动redis服务器 可以将期设置会服务

 进入到目录 输入命令  redis-server --service-install redis.windows.conf --loglevel verbose

redis的命令

1.字符串类型的操作

    set xxx  yyy     存储字符串

  get xxx       获取字符串

  del xxx       删除字符串

  incr xxx        当存储的字符串为整形数字自增

  decr xxx       自减

  incrby xxx  5      每次自增5or    这种自增自减的应用场景 适合做唯一ID的生成

  decrby xxx  5  每次自减5or

 2.散列类型的操作

  Hset user  username yuan      存储一个哈希类型存储的字符串

  Hmset password 123  email 123     存储多个个哈希类型存储的字符串

  hget  user username             获取一个哈希类型存储的字符串

  hmget  user username email           获取多个哈希类型存储的字符串

  hgetall  user                   获取全部哈希类型存储的字符串

 3.列表类型的操作

  lpush mymode  x x x x    从左边开始插入数据

  rpush mymode  x x x x    从右边开始插入数据

  lrange  mymode  0-9/0 -1    查询数据 范围  为-1时查询全部

  rpop mymode        从右边弹出一个

  lpop mymode        从左边弹出一个    应用场景:消息队列、 削峰

 4.集合类型的操作

  sadd mymode x x x1      添加数据 去重

  scard mymode  ——2   查看数据个数 

  smenbers mymode       查看数据

  srem mymode  x  ...       删除存储的数据

  应用场景:独立用户访问

 5.有序集合类型操作

  zadd mymode   1 y  2 d   3  h   4  d   5e  存储有序集合

  zrange mymode              查看有序集合 按从小到大排

  zrevrange mymode             反转 从大到小拍

  应用场景:做排行榜

 通用命令

  keys *    获取所有键

  del x      删除指定键

  exists x    判断是否存在指定键

  type x    获得指定键类型

  expire  x  60  设置键值队存在时间  应用场景:短信验证码

更多命令可以在官方网站上查询文档

3.使用java连接Redis数据库

  首先需要使用的两个jar包

  1.comments-pool2

  2.jedis

使用方法:


package com.itheima.utils;

import redis.clients.jedis.Jedis;

 1 public class RedisUtilSnapshot {
 2     public static void main(String[] args) {
 3         //0. 导入jar包o
 4         //1.  建立与redis的连接
 5         Jedis connection = new Jedis("localhost", 6379);
 6         //2. 使用该连接 执行命令
 7         //connection.set("test","小明");
 8         String s = connection.get("test");
 9         System.out.println(s);
10         //3.关闭连接
11         connection.close();
13     }
14 }

 

  为了提高代码的复用性 创建工具类

 

  初始版本  

 1     private static JedisPool pool =null;
 2     static{
 3         //获取连接  不能自己创建了   首先创建一个连接池
 4         GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
 5         //初始化
 6         poolConfig.setMinIdle(10);
 7         //最大空闲
 8         poolConfig.setMaxIdle(50);
 9         //最多..
10         poolConfig.setMaxTotal(100);
11 
12         poolConfig.setMaxWaitMillis(2000);
13 
14         pool = new JedisPool(poolConfig, "localhost", 6379);
15     }
16 
17     public static void closePool(){
18         pool.close();
19     }
20 
21 
22 
23     public static Jedis getConnection(){
24         //需要连接 从池中借出一个
25         Jedis connection = pool.getResource();
26 
27         return connection;
28 
29     }

 

    优化版本 :创建一个设置连接池的参数的配置文件让工具类获取——properties

properties文件

1 redis.minIdle=10
2 redis.maxIdle=50
3 redis.maxTotal=100
4 redis.maxWaitMillis=2000
5 redis.host=localhost
6 redis.port=6379

工具类

  

 1 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
 2 import redis.clients.jedis.Jedis;
 3 import redis.clients.jedis.JedisPool;
 4 import java.io.IOException;
 5 import java.util.Properties;
 6 /**
 7  * 正式版本
 8  */
 9 public class RedisUtil {
10     private static JedisPool pool =null;
11     static{
12         //获取连接  不能自己创建了   首先创建一个连接池
13         GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
14         Properties properties=new Properties();
15         try {
16             properties.load(RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));
17             int minIdle = Integer.parseInt(properties.getProperty("redis.minIdle"));
18             int maxIdle = Integer.parseInt(properties.getProperty("redis.maxIdle"));
19             int maxTotal = Integer.parseInt(properties.getProperty("redis.maxTotal"));
20             int maxWaitMillis = Integer.parseInt(properties.getProperty("redis.maxWaitMillis"));
21             String host = properties.getProperty("redis.host");
22             int port = Integer.parseInt(properties.getProperty("redis.port"));
23             //初始化
24             poolConfig.setMinIdle(minIdle);
25             //最大空闲
26             poolConfig.setMaxIdle(maxIdle);
27             //最多..
28             poolConfig.setMaxTotal(maxTotal);
29 
30             poolConfig.setMaxWaitMillis(maxWaitMillis);
31 
32             pool = new JedisPool(poolConfig, host, port);
33 
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37 
38     }
39     public static void closePool(){
40         pool.close();
41     }
42     public static Jedis getConnection(){
43         //需要连接 从池中借出一个
44         Jedis connection = pool.getResource();
45 
46         return connection;
47     }

在读取properties文件时也可以使用专门读取该文件的类ResourceBundle

rb=ResourceBundle.getBundle("xx"); rb.getString("redis.xxx")

 

4.Redis的持久化策略

有多种防止存储数据消失的策略

1.防止意外中断的

  RDB持久化(默认支持,无需配置) 该机制是指在指定的时间间隔内将内存中的数据集快照写入磁盘。 

  Sava  tims

2.备份命令

  AOF持久化该机制将以日志的形式记录服务器所处理的每一个写操作,在Redis服务器启动之初会读取

  该文件来重新构建 数据库,以保证启动后数据库中的数据是完整的

   appendfsync  always/ervysec/no

 

总结:

  Redis数据库的技术现在来说还是很复杂

    

 

 

 在前期的使用中大概了解些概念 在真正需要用到时  需要解决问题方案时可以通过百度查询文档继续深入学习 

 

posted @ 2020-03-19 11:40  袁德华  阅读(125)  评论(0)    收藏  举报