Redis项目案例

实战案例

  • 基于redis缓存实现用户增删改查的项目分析
    • 需求
    • 项目运行环境
    • 项目搭建
      • 1、父工程
      • 2、子工程
    • 运行效果

基于redis缓存实现用户增删改查的项目分析

需求

redis的作用是实现数据的缓存,以此来提升系统性能。
在本案例中,基本功能是实现用户的增删改查,主要在用户的查询和修改时用到了redis的缓存。

  • 业务逻辑 -查询:首先对接收到的userid在redis缓存中查询,如果没有在查询mysql数据库,查询到以后将查询结果封装成json类型发送给redis进行缓存,方便下次读取
  • 业务逻辑 -更新:执行更新操作后,需要将redis中原来的缓存也一并删除,并将更新过的数据重新插入redis缓存

项目运行环境

1、在运行项目时,需要打开单机版redis,以及redis图形化工具RedisDesktopManager(方便查看数据变更信息)
2、需要关闭防火墙
3、需要有mysql或者oracle数据库

项目搭建

Redis项目案例_第1张图片

1、父工程

采取maven来构建项目,父工程为pom工程,并且添加以下坐标,并对其版本进行控制

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.bjsxtgroupId>
  <artifactId>parentartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>pompackaging>
 
 	
	<properties>
		<jedis.version>2.9.0jedis.version>
		<jedis.version>2.9.0jedis.version>
		<junit.version>4.12junit.version>
		<spring.version>4.1.3.RELEASEspring.version>
		<mybatis.version>3.2.8mybatis.version>
		<mybatis.spring.version>1.2.2mybatis.spring.version>
		<mysql.version>5.1.32mysql.version>
		<slf4j.version>1.6.4slf4j.version>
		<druid.version>1.0.9druid.version>
		<jstl.version>1.2jstl.version>
		<servlet-api.version>2.5servlet-api.version>
		<tomcat.version>2.2tomcat.version>
		<jsp-api.version>2.0jsp-api.version>
		<jackson.version>2.4.2jackson.version>
		
	properties>


	
	<dependencyManagement>
		
		<dependencies>
		
		
		<dependency>
			<groupId>redis.clientsgroupId>
			<artifactId>jedisartifactId>
			<version>${jedis.version}version>
		dependency>
		
			
			<dependency>
				<groupId>junitgroupId>
				<artifactId>junitartifactId>
				<version>${junit.version}version>
			dependency>
			
			<dependency>
				<groupId>org.slf4jgroupId>
				<artifactId>slf4j-log4j12artifactId>
				<version>${slf4j.version}version>
			dependency>
			
			<dependency>
				<groupId>org.mybatisgroupId>
				<artifactId>mybatisartifactId>
				<version>${mybatis.version}version>
			dependency>
			<dependency>
				<groupId>org.mybatisgroupId>
				<artifactId>mybatis-springartifactId>
				<version>${mybatis.spring.version}version>
			dependency>
			
			<dependency>
				<groupId>mysqlgroupId>
				<artifactId>mysql-connector-javaartifactId>
				<version>${mysql.version}version>
			dependency>
			
			<dependency>
				<groupId>com.alibabagroupId>
				<artifactId>druidartifactId>
				<version>${druid.version}version>
			dependency>
			
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-contextartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-beansartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-webmvcartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-jdbcartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-aspectsartifactId>
				<version>${spring.version}version>
			dependency>
			
			<dependency>
				<groupId>jstlgroupId>
				<artifactId>jstlartifactId>
				<version>${jstl.version}version>
			dependency>
			<dependency>
				<groupId>javax.servletgroupId>
				<artifactId>servlet-apiartifactId>
				<version>${servlet-api.version}version>
				<scope>providedscope>
			dependency>
			<dependency>
				<groupId>javax.servletgroupId>
				<artifactId>jsp-apiartifactId>
				<version>${jsp-api.version}version>
				<scope>providedscope>
			dependency>
			
			
			<dependency>
				<groupId>com.fasterxml.jackson.coregroupId>
				<artifactId>jackson-databindartifactId>
				<version>${jackson.version}version>
			dependency>
		dependencies>
	dependencyManagement>
	
<build>
		<resources>
			<resource>
				<directory>src/main/javadirectory>
				<includes>
					<include>**/*.xmlinclude>
				includes>
			resource>
			<resource>
				<directory>src/main/resourcesdirectory>
				<includes>
					<include>**/*.xmlinclude>
					<include>**/*.propertiesinclude>
				includes>
			resource>
		resources>
		
		<pluginManagement>
			<plugins>
				
				<plugin>
					<groupId>org.apache.tomcat.mavengroupId>
					<artifactId>tomcat7-maven-pluginartifactId>
					<version>${tomcat.version}version>
				plugin>
			plugins>
		pluginManagement>
	build>
project>

2、子工程

jar工程,进行业务功能的具体实现,下面将主要粘贴一些重要代码

  • JedisDao 与其实现类
package com.bjsxt.jedisdao;

public interface JedisDao {
	
	 String set(String key,String value);
	 String get(String key);
	 
	 Long hset(String key,String field,String value);
	 String hget(String key,String field);
	 
	 /**
	  * 用于设置失效时间
	  * @param key
	  * @param sec
	  * @return
	  */
	 Long expire(String key,int sec);
	 /**
	  * 删除jedis缓存
	  * @param key
	  * @return
	  */
	 Long del(String key);
}



package com.bjsxt.jedisdao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.jedisdao.JedisDao;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class JedisDaoSingleImpl implements JedisDao{
	//使用@Autowired要开启组件扫描,不使用的话要通过pagebean的propertie注入进来
	@Autowired
	private JedisPool jedispool;
	
	@Override
	public String set(String key, String value) {
		Jedis resource=jedispool.getResource();
		return resource.set(key, value);
	}

	@Override
	public String get(String key) {
		Jedis resource=jedispool.getResource();
		return resource.get(key);
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis resource=jedispool.getResource();
		return resource.hset(key, field, value);
	}

	@Override
	public String hget(String key, String field) {
		Jedis resource=jedispool.getResource();
		return resource.hget(key, field);
	}

	@Override
	public Long expire(String key, int sec) {
		Jedis resource = jedispool.getResource();
		
		return resource.expire(key, sec);
	}

	@Override
	public Long del(String key) {
		Jedis resource = jedispool.getResource();
		
		return resource.del(key);
	}

}

  • 业务实现类(核心代码),实现用户的增删改查并使用redis缓存对系统进行优化
package com.bjsxt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bjsxt.commons.JsonUtils;
import com.bjsxt.jedisdao.JedisDao;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;

import redis.clients.jedis.JedisPool;
/**
 * 业务逻辑 -查询:首先对接受到的userid在redis数据库中查询,如果没有在查询mysql数据库,查询以后将拆线呢结果封装成json类型发送给redis缓存
 *  业务逻辑 -更新:执行更新操作后,需要将redis中的缓存也一并删除,并将更新过的数据重新插入redis缓存
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UsersMapper userMapper;	
	@Autowired
	private JedisDao jedisDao;
	//@Autowired
	//private JedisPool jedisPool;
	
	@Override
	public void addUser(Users users) {
		this.userMapper.insertUsers(users);
	}
	@Override
	public void updateUser(Users users) {
		this.userMapper.updateUser(users);
		int userid = users.getUserid();
		
		try {
			//删除原来的redis缓存
			this.jedisDao.del(this.REDIS_USERS_PRIFX+":"+userid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			//删除后将更新过后的信息放入redis
			String objectToJson = JsonUtils.objectToJson(users);
			//String redis_Key = this.REDIS_USERS_PRIFX+":"+userid;
			jedisDao.set(this.REDIS_USERS_PRIFX+":"+userid, objectToJson);
			jedisDao.expire(this.REDIS_USERS_PRIFX+":"+userid,100);//如果设置失败,最好clean一下
			//jedisPool.getResource().expire(redis_Key, 20);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//添加了一个配置文件内容为:REDIS_USERS_PRIFX=REDIS_USERS_PRIFX
	//@Value,获取配置文件中相关参数的值
	@Value("${REDIS_USERS_PRIFX}")
	private String REDIS_USERS_PRIFX;
	
	@Override
	public Users findByUserid(int userid) {
		
		try {
			//查看缓存
			String json=this.jedisDao.get(this.REDIS_USERS_PRIFX+":"+userid );//p拼接成json类型字符串key:value
			//在缓存中是否命中
			if (json != null && json.length()>0) {//命中
				System.out.println("-----------------调用查看redis缓存操作--------------------");
				Users users=JsonUtils.jsonToPojo(json, Users.class);
				return users;
			}
		} catch (Exception e) {//保证不影响功能正常实现,需要用try。。。catch
			e.printStackTrace();
		}
		
		
		//查询数据库
		Users users=this.userMapper.selByUserId(userid);
		System.out.println(users);
		System.out.println("==================查询mysql并放入redis缓存=========================");
		try {
			//放入redis中
			String objectToJson = JsonUtils.objectToJson(users);
			//String redis_Key = this.REDIS_USERS_PRIFX+":"+userid;
			jedisDao.set(this.REDIS_USERS_PRIFX+":"+userid, objectToJson);
			jedisDao.expire(this.REDIS_USERS_PRIFX+":"+userid,60);//如果设置失败,最好clean一下
			//jedisPool.getResource().expire(redis_Key, 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return users;
		
		
	}
	
}

  • 扫描配置文件,在application-dao.xml配置文件中添加如下代码

  
      <context:property-placeholder location="classpath:resource/*.properties"/> 
  • 令redis生效的applicationContext-jedis.xml配置文件

<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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	
	
	<bean id="poolconfig" 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" />
		
		<property name="maxWaitMillis" value="1500" />
		
		<property name="testOnBorrow" value="true" />
		
		<property name="testWhileIdle" value="true" />
		
		<property name="blockWhenExhausted" value="false" />
		
	bean>
	
	
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="poolConfig">
			<ref bean="poolconfig">ref>
		constructor-arg>
		<constructor-arg name="host">
			<value>192.168.179.131value>
		constructor-arg>
		<constructor-arg name="port">
			<value>6379value>
		constructor-arg>
	bean>
	
	 <bean id="jedisDaoSingleImpl" class="com.bjsxt.jedisdao.impl.JedisDaoSingleImpl">bean> 
	
	
	<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8001value>
					constructor-arg>
				bean>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8002value>
					constructor-arg>
				bean>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8003value>
					constructor-arg>
				bean>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8004value>
					constructor-arg>
				bean>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8005value>
					constructor-arg>
				bean>
				<bean name="hostAndPort" class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host">
						<value>192.168.179.131value>
					constructor-arg>
					<constructor-arg name="port">
						<value>8006value>
					constructor-arg>
				bean>
				
			set>
			
		constructor-arg>
		<constructor-arg name="poolConfig">
			<ref bean="poolconfig">ref>
		constructor-arg>
	bean>
	
	
beans>

运行效果

第一次查询id为1的用户
Redis项目案例_第2张图片
Redis项目案例_第3张图片
Redis项目案例_第4张图片
第二次查询id为1的用户(相当于刷新页面)
Redis项目案例_第5张图片
修改数据:

Redis项目案例_第6张图片
redis客户端显示的结果
Redis项目案例_第7张图片

项目源码~~~
链接:https://pan.baidu.com/s/1Bat7QS6akuSF4k6MprIFiw
提取码:z23d
复制这段内容后打开百度网盘手机App,操作更方便哦

你可能感兴趣的:(Java项目,Java高级技术)