Redis 入门 慕课网,笔记

来源:互联网 发布:linux 安装vnc server 编辑:程序博客网 时间:2024/06/06 01:12

01-NoSQL的概述

NoSQL = Not Only SQL
非关系型数据库


为什么需要NoSQL,
高并发读写
海量数据的高效率存储和访问
高可扩展性和高可用性


NoSQL数据库的四大分类


键值(Key-Value)存储


02-NoSQL的概述



应用场景
缓存
任务队列
网址访问统计
数据过期处理
应用排行榜
分布式集群结构中的session分离


03-Redis的安装

搭建环境
虚拟机:VMware 10.0.02
linux系统:centOS-6.5
SSH客户端:SecureCRT 7.3 SecureFx7.3


安装过程:
 
 用su命令改变用户
(1)安装编译器:yum install gcc-c++ (2)进入root目录, wget http://download.redis.io/releases/redis-3.0.7.tar.gz
  (3)解压Redis压缩包:tar -zxvf redis-3.0.7.tar.gz
  (4)进入Redis目录进行编译:make  (5)安装Redis,指定安装目录为/usr/local/redis:make PREFIX=/usr/local/redis install  (6)将redis.conf拷贝到Redis安装目录:cp redis.conf /usr/local/redis
  (7)进入安装目录,更改redis.conf文件:vim redis.conf --> daemonize no 改为 yes
  (8)启动redis前端模式,该模式命令窗口始终被占用: ./redis-server  (9)启动redis后端模式:./bin/redis-server redis.conf
  (10)查看redis是否启动: ps -ef|grep -i redis 
  (11)关闭redis 。方法一,kill -9 方法二: ./bin/redis-cli shutdown


5-1 Jedis入门

java调用redis服务,推荐使用Jedis

pom.xml

<dependencies><dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.12</version>    <scope>test</scope></dependency><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-pool2</artifactId>    <version>2.3</version></dependency> <dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.7.0</version></dependency>  </dependencies>


测试方法
package com.immoc.jedis;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisDemo1 {@Testpublic void demo1(){//1.设置IP地址和端口Jedis jedis = new Jedis("192.168.136.130",6379);//2.保存数据jedis.set("age", "100");//3.获取数据String age = jedis.get("age");System.out.println(age);//4.释放资源jedis.close();}@Test/** * 使用连接池 */public void demo2(){//获取连接池的配置对象JedisPoolConfig config = new JedisPoolConfig();//设置最大连接数config.setMaxTotal(30);//设置最大空闲连接数config.setMaxIdle(10);//获得连接池JedisPool jedisPool = new JedisPool(config,"192.168.136.130",6379);//获取核心对象Jedis jedis = null;try{//通过连接池或得连接jedis = jedisPool.getResource();//设置数据jedis.set("name", "张三");//获取数据String name = jedis.get("name");System.out.println(name);}catch(Exception e){e.printStackTrace();}finally{//释放资源if(jedis != null){jedis.close();}if(jedisPool != null){jedisPool.close();}}}}


服务器,需要保证6379端口,开启

开启的方法

vim /etc/sysconfig/iptables


启动防火墙:service iptables restart

查看开放的端口:netstat -tln



6-1 redis数据结果之字符串



 6-2.数据结构之哈希

命令:myhash username jack
解释:定义Hash变量,key是username;value是jack

命令:hmset myhash2 username rose age 21
解释:定义多个Hash变量

命令:hget myhash username
解释:得到Hash变量,key是username的值

命令:hmget myhash username age
解释:得到Hash变量,可以指定多个key值

命令:hgetall myhash
解释:得到myhash的所有key和value值

命令:hdel myhash2 username age
解释:删除myhash2的多个key

命令:del myhash2
解释:直接删除变量myhash2

命令:hincrby myhash age 5
解释:变量age值增加5

命令:hexists myhash username
解释:判断myhash是否存在key值username。如果存在,就返回1;如果不存在,就返回0;





  6-3.数据结构之list
存储list
ArrayList使用数组方式
LinkedList使用双向链表
双向链表中添加数据
双向链表中删除数据


lpush 从左侧添加数据到链表
rpush 从右侧添加数据到链表
lpop 左侧弹出,弹出后,链表中就不存在了
rpop 右侧弹出
llen 查看链表长度,如果参数不存在,返回0
lpushx 左侧插入
lrom 删除列表
lset list 3 mm :往链表list里面第3个位置里插入mm
linsert list4 before b ll :往链表list4里面元素b的前面插入ll
linsert list4 after b 22:在b后面插入
rpoplpush list5 list6 :把list右边元素弹出,插入到list6的左侧



  6-4.数据结构之set
存储Set
和List类型不同的是,Set集合中不允许出现重复的元素
存储set常用命令
sadd:添加
srem:删除
smembers:查看参数
sismember:判断是否存在指定元素
sdiff:差集运算
sinter:交集运算
sunion:并集运算
sdiffstore my1 mya1 myb1:把集合mya1和myb1的差集交给变量my1
sinterstore
sunionstore


  6-5.数据结构之sorted-set
Sorted-Set中的成员在集合中的位置是有序的


zadd 添加
zscore 查看值
zcard 查看长度
zrem 删除
zrange mysort 0 -1 withscores:根据范围查找,0:最开始;-1:最后一个.widthscores是带着数组显示
zrevrange mysort 0 -1 widthscores:排序
zremrangebyrank:根据范围删除
zremrangebyscore:根据score的范围删除



  7-1.keys的通用操作



keys *:或得所有keys:
del:删除
exists:是否存在,存在返回1,不存在返回0
rename:重命名
expire:设置过期时间
ttl:查看剩下时间
type:查看类型



  8-1.redis的特性



多数据库


默认选择0号数据库。一共有0到15个数据库
select:选择数据库
move:移动到哪个数据库
multi exec discard:实现事务
multi:开启事务
exec:提交
discard:回滚



  9-1.redis的持久化的概述

RDB持久化
默认,在指定的时间间隔里面,把数据写入
AOF持久化


无持久化


同时使用RDB和AOF


  9-2. 持久化的RDB的方式


  9-3.持久化的AOF的方式
  


spring应用场景

spring-redis.xml


<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName" default-lazy-init="false"><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="50" />        <property name="maxIdle" value="10" />        <property name="maxWaitMillis" value="1000" />        <property name="testOnBorrow" value="true" /></bean><bean id="jedisPool" class="redis.clients.jedis.JedisPool">        <constructor-arg index="0" ref="jedisPoolConfig"/>        <constructor-arg index="1" value="${redis.host}"/>        <constructor-arg index="2" value="${redis.port}"/>        <constructor-arg index="3" value="1000"/>        <constructor-arg index="4" value="${redis.pwd}"/>    </bean></beans>


RedisService.java

package com.wjtc.wechat.store.service.impl;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import com.wjtc.core.util.HttpClientUtil;import com.wjtc.wechat.common.bean.other.WxAuthorizer;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;@Componentpublic class RedisService {@Autowiredprivate JedisPool jedisPool;@Value("${wechat_token_url}")private String wechat_token_url;/** * 根据公众号的appid获取公众号的令牌 * @param appid 公众号的appid * @return */public String get(WxAuthorizer author) {String token = null;Jedis jedis = null;try {jedis = jedisPool.getResource();token = jedis.get(author.getAppid());} catch (Exception e) {e.printStackTrace();} finally {if (jedis != null) {jedis.close();}}if (StringUtils.isBlank(token)) {token = HttpClientUtil.doGet(wechat_token_url + "?authorizer_appid=" + author.getAppid() + "&authorizer_refresh_token=" + author.getRefresh_token());System.out.println("token=" + token);if (StringUtils.isBlank(token) || "error".equals(token)) {token = null;return token;}}token = token.replace("\"", "");return token;}}






原创粉丝点击