互联网框架redis学习(自学)

linux启动和关闭

[root@localhost02 bin]#./redis-server redis.conf (后端启动)
[root@localhost02 bin]# ./redis-cli shutdown   后台启动对应的关闭方法
  1. 什么是redis
    redis是一个nosql(not only sql不仅仅只有sql)数据库.翻译成中文叫做非关系型型数据库.
    关系型数据库:以二维表形式存储数据
    非关系型数据库: 以键值对形式存储数据(key, value形式)
    是一家意大利的创业公司出的,然后后来这家公司被VMware赞助. redis底层用C语句编写.

    redis是将数据存放到内存中,由于内容存取速度快所以redis被广泛应用在互联网项目中,
    redis有点:存取速度快,官方称读取速度会达到30万次每秒,写速度在10万次每秒最有,具体限制于硬件.
    缺点:对持久化支持不够良好,
    所以redis一般不作为数据的主数据库存储,一般配合传统的关系型数据库使用.

  2. redis应用领域
    分布式缓存
    分布式session
    保存博客或者论坛的留言回复等.
    总之是用在数据量大,并发量高的情况下

  3. 怎么用
    redis主要就是使用命令来进行操作,java端在代码中可以使用Jedis来操作redis服务器
    redis数据类型
    字符串String
    列表list redis中使用的是双向循环链表来实现的list,在redis中更像栈
    散列Hash 一般应用于将redis作为分布式缓存,存储数据库中的数据对象
    集合set set中数据是无序的并且不允许重复
    有序集合zset redis会根据分数自动排序,这里可以使用在学生成绩排序,
    或者是手机应用商店流行软件排名等需求中
    4.redis持久化方案:
    rdb:可以设置间隔多长时间保存一次(Redis不用任何配置默认的持久化方案)
    有点:让redis的数据存取速度变快
    缺点:服务器断电时会丢失部分数据(数据的完整性得不到保证)
    aof:可以设置实时保存
    优点:持久化良好,能包装数据的完整性
    缺点:大大降低了redis系统的存取速度

  4. 主从复制:
    这里使用了心跳检测机制,主从复制必须使用rdb持久化方式
    从服务器一般是只读的,保证主服务器和从服务器的数据一致性

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

安装gcc很可能会出现问题(centos6.5),查看文章【YumRepo Error: All mirror URLs are not using ftp, http[s] or file.centos6 yum失败!】解决

ping 192.168.43.93
[root@localhost02 ~]# sed -i "s|enabled=1|enabled=0|g" /etc/yum/pluginconf.d/fastestmirror.conf

[root@localhost02 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@localhost02 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo

[root@localhost02 ~]# yum install gcc-c++

在这里插入图片描述
在这里插入图片描述

[root@localhost02 ~]# ls
anaconda-ks.cfg  anaconda-screenshots  install.log  install.log.syslog  redis-3.0.7.tar.gz


[root@localhost02 redis]# ls
redis-3.0.7  redis-3.0.7.tar.gz
[root@localhost02 local]mkdir redis(redis目录)
[root@localhost02 local]cp /root/redis-3.0.7.tar.gz /usr/local/redis/(拷贝)
[root@localhost02 redis]#chmod +x redis-3.0.7.tar.gz
(加权限)  
[root@localhost02 redis]tar -zxvf redis-3.0.7.tar.gz
(解压)
[root@localhost02 redis]# cd redis-3.0.7
[root@localhost02 redis-3.0.7]# make (编译)
[root@localhost02 redis-3.0.7]make PREFIX=/usr/local/redis install
[root@localhost02 redis]# cd bin
[root@localhost02 bin]# ./redis-server(前端启动)(ctrl c即可关闭)
[root@localhost02 redis-3.0.7]# cp redis.conf  /usr/local/redis/bin/
[root@localhost02 bin]# chmod 777 redis.conf(加权限)
[root@localhost02 bin]# vi redis.conf  编写设置后台启动
(dzemonize yes)
[root@localhost02 bin]#./redis-server redis.conf (后端启动)
[root@localhost02 bin]# ./redis-cli shutdown   (后台启动对应的关闭方法)

[root@localhost02 bin]# ./redis-cli(客户端)
127.0.0.1:6379> set key1 1(设置值)
OK
127.0.0.1:6379> get key1(获取值)
"1"

Jedis连接redis会出现连接超时的效果,导致无法操作redis
可以关闭Linux防火墙/etc/init.d/iptables stop(暂时关闭)
/etc/init.d/iptables status(查看防火墙)

命令:
删除
127.0.0.1:6379> del key4
自增
127.0.0.1:6379> incr key1
(integer) 2
加法
127.0.0.1:6379> incrby key1 10
(integer) 12

多层map
hash:
127.0.0.1:6379> hset user1 name zhangsan
(integer) 1
127.0.0.1:6379> hset user1 age 18
(integer) 1
127.0.0.1:6379> hset user1 sex 1
127.0.0.1:6379> hget user1 sex
"1"
127.0.0.1:6379> hmset user1 name lisi age 19 sex 0(批量设置值)
OK
127.0.0.1:6379> hmget user1 name age sex(批量获取值)
1) "lisi"
2) "19"
3) "0"

127.0.0.1:6379> hgetall user1(全部获取)
1) "name"
2) "lisi"
3) "age"
4) "19"
5) "sex"
6) "0"

127.0.0.1:6379> hdel user2 sex
(integer) 1
127.0.0.1:6379> hgetall user2
1) "name"
2) "zhangsan"
3) "age"
4) "19"

127.0.0.1:6379> hincrby user1 age 1(加法)
(integer) 20

list:()
127.0.0.1:6379> lpush lkey1 1 2 3(存入值)
(integer) 3
127.0.0.1:6379> lrange lkey1 0 2(获取值,通过下标【0-2】)
1) "3"
2) "2"
3) "1"

127.0.0.1:6379> lrange lkey1 0 -1(全部取出)
1) "3"
2) "2"
3) "1"

127.0.0.1:6379> lpush lkey1 4 5 6(左存入【常用】)
(integer) 6
127.0.0.1:6379> lrange lkey1 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"

127.0.0.1:6379> rpush lkey1 7 8 9(右存入【不常用】)
(integer) 9
127.0.0.1:6379> lrange lkey1 0 -1(右取出值)
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
7) "7"
8) "8"
9) "9"

127.0.0.1:6379> lpop lkey1(左删除一个值)
"6"
127.0.0.1:6379> lpop lkey1(左再删除一个值)
"5"
127.0.0.1:6379> lrange lkey1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "7"
6) "8"
7) "9"

127.0.0.1:6379> rpop lkey1(右删除一个值)
"9"
127.0.0.1:6379> rpop lkey1
"8"
127.0.0.1:6379> lrange lkey1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "7"
127.0.0.1:6379> llen lkey1(获取长度)
(integer) 5

set:
127.0.0.1:6379> sadd skey1 a 2 3 4 5(存入值)
(integer) 5
127.0.0.1:6379> smembers skey1(获取值)
1) "4"
2) "a"
3) "3"
4) "2"
5) "5"
127.0.0.1:6379> srem skey1 5(删除5)
(integer) 1
127.0.0.1:6379> smembers skey1
1) "a"
2) "3"
3) "2"
4) "4"
127.0.0.1:6379> sismember skey1 4(是否有4)
(integer) 1

127.0.0.1:6379> sdiff skey1 skey2(skey1但是skey2没有)
1) "a"
2) "2"
127.0.0.1:6379> sinter skey1 skey2(交集)
1) "3"
2) "4"
127.0.0.1:6379> sunion skey1 skey2(并集)
1) "2"
2) "5"
3) "3"
4) "4"
5) "6"
6) "a"

分数:
127.0.0.1:6379> zadd zkey2 10 zhangsan 20 lisi 30 wangwu
(integer) 3
127.0.0.1:6379> zrange zkey2 0 -1(取值【升序】)
1) "zhangsan"
2) "lisi"
3) "wangwu"
127.0.0.1:6379> zrange zkey2 0 -1 withscores(取值带分数【升序】)
1) "zhangsan"
2) "10"
3) "lisi"
4) "20"
5) "wangwu"
6) "30"
127.0.0.1:6379> zrem zkey2 wangwu(删除wangwu)
(integer) 1
127.0.0.1:6379> zrange zkey2 0 -1
1) "zhangsan"
2) "lisi"
127.0.0.1:6379> zscore zkey2 lisi(获取lisi成绩)
"20"
127.0.0.1:6379> zrevrange zkey2 0 -1(获取成绩【降序】)
1) "lisi"
2) "zhangsan"
127.0.0.1:6379> zrevrange zkey2 0 -1 withscores(获取成绩带分数【降序】)
1) "lisi"
2) "20"
3) "zhangsan"
4) "10"

127.0.0.1:6379> keys *(获取所有key)
 1) "lkey1"
 2) "key1"
 3) "key01"
 4) "user2"
 5) "zkey2"
 6) "key3"
 7) "skey1"
 8) "key2"
 9) "user1"
10) "skey2"
127.0.0.1:6379> keys *1(获取带1的key)
1) "lkey1"
2) "key1"
3) "key01"
4) "skey1"
5) "user1"
127.0.0.1:6379> ping(作响应pong)
PONG
127.0.0.1:6379> rm -f dump.rbd(删除rdb里的全部数据)

更改为aof方式(先关闭服务。更改后开启服务)

在这里插入图片描述

Linux中编辑(/app 进行查找app开头的字段)
在这里插入图片描述
注意:改为aof效果不好,需要改回去rdb

ps -aux|grp redis显示所有包含其他使用者的行程

主从复制
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:

package cn.tedu.redis;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisSpringTest {
	private ApplicationContext applicationContext;
	@Before
	public void setUp() throws Exception{
		String configLocation="classpath:ApplicationContext.xml";
		applicationContext=new ClassPathXmlApplicationContext(configLocation);
		
	}
	@Test
	public void testJedisSpring() throws Exception{
		//获取连接池
		JedisPool jedisPool=(JedisPool) applicationContext.getBean("jedisPool");
		//获取连接
		Jedis jedis=jedisPool.getResource();
		//存入
		jedis.set("key4", "fff");
		//取出
		System.out.println(jedis.get("key4"));
	}
}

package cn.tedu.redis;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;



public class JedisTest {
	@Test
	public void testJedis1() throws Exception{
		//创建和redis的连接
		Jedis jedis=new Jedis("192.168.253.129",6379);
		//存入
		jedis.set("key2", "中国");
		//取出
		System.out.println(jedis.get("key2"));
		//关闭
		jedis.close();
	}
	@Test
	public void testJedisPool() throws Exception{
		//创建连接池
		JedisPool pool=new JedisPool("192.168.253.129",6379);
		//获取连接
		Jedis jedis=pool.getResource();
		//存入
		jedis.set("key3", "3");
		//取出
		System.out.println(jedis.get("key3"));
		//使用连接时,使用完毕要关闭,关闭后连接会自动回到连接池供别人使用,
		//如果一直不关闭,被耗尽后会死机
		jedis.close();
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 连接池配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow" value="true" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle" value="true" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted" value="false" />
	</bean>
	
	<!-- redis单机 通过连接池 -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
		<constructor-arg name="host" value="192.168.253.129"/>
		<constructor-arg name="port" value="6379"/>
	</bean>
</beans>

结构和jar包(D:\未来\互联网框架\redis\参考资料redis\bin\jedis\全jar包)
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值