您好,欢迎来到好走旅游网。
搜索
您的当前位置:首页Redis服务器的使用

Redis服务器的使用

来源:好走旅游网

安装

需要注意的是:(设置服务前如果redis服务在开着 要先关闭redis服务 不然后面生成不了redis-6379.pid,可以查看redis服务进程 关闭杀死resid服务)

设置Redis开机自启

1.在/etc下新建redis文件夹

cp /usr/local/redis-5.0.4/redis.conf /etc/redis/6379.conf

3.复制redis启动脚本到/etc/init.d/redis文件中

cp /usr/local/redis-5.0.4/utils/redis_init_script /etc/init.d/redis

4.修改启动脚本参数

vi /etc/init.d/redis

在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加

# chkconfig: 2345 10 90  

# description: Start and Stop redis

如图

同时还要修改参数,指定redis的安装路径

5.启动

打开redis命令:service redis start

关闭redis命令:service redis stop

设为开机启动:chkconfig redis on

设为开机关闭:chkconfig redis off

Redis缓存Demo

依赖导入spring和redis相关jar包

<dependencies>
	<!-- Spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
	</dependency>
	<!-- Redis客户端 -->
	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-redis</artifactId>
	</dependency>
</dependencies>

创建redis.properties文件,保存公共信息

redis_host=192.168.220.128
redis_port=6379
redis_maxIdle=300
redis_maxWait=3000
redis_maxTotal=4

创建配置文件applicationContext-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" 
		xmlns:p="http://www.springframework.org/schema/p"
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath*:properties/redis.properties"/>
	
	<!-- redis相关配置 -->
	<!-- 获取连接池poolConfig -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis_maxIdle}"></property>
		<property name="maxWaitMillis" value="${redis_maxWait}"></property>
		<property name="maxTotal" value="${redis_maxTotal}"></property>
	</bean>
	<!-- 获取连接池工厂 -->
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
		p:host-name="${redis_host}" p:port="${redis_port}" p:pool-config-ref="poolConfig">
	</bean>
	<!-- Jedis模板 -->
	<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory"></property>
	</bean>
</beans>
package com.offcn.demo;

import java.util.Set;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

public class Demo {

	public static void main(String[] args) {
		//读取配置文件
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
		//获取模板对象
		RedisTemplate<String,String> template = (RedisTemplate<String, String>) context.getBean("jedisTemplate");
		//赋值
		template.boundSetOps("name").add("xiaocang");
		//取出相同name的所有值
		Set<String> members = template.boundSetOps("name").members();
		System.out.println(members);
		//取出相同name的一个值
		String pop = template.boundSetOps("name").pop();
		System.out.println(pop);
	}

}

从左装数据和丛有装数据是以0位界限的.

package com.offcn.demo;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

public class Demo3 {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-redis.xml");
		RedisTemplate<String,String> template = (RedisTemplate<String, String>) context.getBean("jedisTemplate");
		//从右开始装数据
//		template.boundListOps("person").rightPush("小苍");
//		template.boundListOps("person").rightPush("小泽");
//		template.boundListOps("person").rightPush("小西");
//		template.boundListOps("person").rightPush("小东");
		
//		List<String> range = template.boundListOps("person").range(0, 1);
//		System.out.println(range);
//		template.delete("person");
//		System.out.println(template.boundListOps("person").range(0,100));
		
		//从左边装数据
//		template.boundListOps("person").leftPush("小苍left");
//		template.boundListOps("person").leftPush("小泽left");
//		template.boundListOps("person").leftPush("小西left");
//		template.boundListOps("person").leftPush("小东left");
//		
//		template.delete("name");
//		template.delete("person");
//		System.out.println(template.boundListOps("person").range(0, 100));
		
		//下标取值
//		String index = template.boundListOps("person").index(0);
//		System.out.println(index);
//		
//		template.boundListOps("person").remove(0, "小东left");
//		System.out.println(template.boundListOps("person").range(0, 100));
		
		template.boundHashOps("person").put("name", "小苍");
		template.boundHashOps("person").put("age", "小泽");
		System.out.println(template.boundHashOps("person").get("name"));
		System.out.println(template.boundHashOps("person").values());
	}
}

首页轮播广告添加缓存

 

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务