Redis--项目实战

spring+redis

配置

pom文件配置
项目中使用的是SpringDataRedis,所以需要引用SpringDataRedis的jar包,与此同时,SpringDataRedis真正和redis链接的是jedis,所以也需要引用jedis的jar包

<dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
</dependency>
<dependency>
       <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
</dependency>

spring-redis.xml
配置的解释如下:
第一种方式:注解
第二种方式:Redistemplate
公共部分:
jedisconnectionFactory
key、value的序列化

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 启用缓存注解功能 -->
    <cache:annotation-driven cache-manager="redisCacheManager"/>

    <!---redis单机版配置-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg>
            <bean class="org.springframework.data.redis.connection.RedisStandaloneConfiguration"
                  c:host-name="192.168.22.60" c:port="6379"/>
        </constructor-arg>
    </bean>

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">        <!-- 最小空闲数 -->
        <property name="minIdle" value="5"/>        <!-- 最大空闲数 -->
        <property name="maxIdle" value="100"/>        <!-- 最大连接数 -->
        <property name="maxTotal" value="300"/>        <!-- 最大等待时间 单位毫秒(ms) -->
        <property name="maxWaitMillis" value="3000"/>        <!-- 使用连接时测试连接是否可用 -->
        <property name="testOnBorrow" value="true"/>
    </bean>


    <!--缓存key和value序列化配置-->
    <bean id="redisCacheManager" class="com.dmsdbj.itoo.tool.redis.TedisCacheManager"
          factory-method="create"
          c:connection-factory-ref="jedisConnectionFactory">
        <property name="keySerializer" ref="keySerializer"/>
        <property name="valueSerializer" ref="valueSerializer"/>
    </bean>
    <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="valueSerializer" class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>

    <!-- redis模板 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer" ref="keySerializer"/>
        <property name="valueSerializer" ref="valueSerializer"/>
    </bean>

    <!-- Session共享 -->
    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
        <property name="maxInactiveIntervalInSeconds" value="7200" />
    </bean>
</beans>

核心代码

以string类型的set为例,其他的用法类似,需要的话去官网上查找即可

@Resource
private RedisTemplate redisTemplate;
String kyeName = REDIS_KEY_PRO_NAME + "_" + examineeId + "_" + templeatId + "_" + ConstantUtils.REDIS_PAPER_NAME;
 redisTemplate.opsForValue().set(kyeName, examPaperModel, 3600 * 4, TimeUnit.SECONDS);

springboot+redis

配置

pom文件
这里默认使用的是lettuce

<dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

yml文件配置

redis:
	host:192.168.22.60
	port:6379
	database:0
	password:
	timeout:300000ms
	jedis:
		pool:
			max-active:8
			max-wait:-1ms
			max-idle:500

RedisConfig文件的配置
这个文件的配置和spring-redis的配置是相同的,只不过一种是spring中xml文件的配置,一种是spring中java文件的配置
这个问题如果不够清晰可以查看一下博客:
https://blog.csdn.net/Sunny5319/article/details/90740358

核心代码

代码同上,并无区别

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值