Redis缓存在项目中的使用

本文探讨了Redis在项目中的应用,包括配置方法——单机与集群版的配置,通过接口和实现方式来讲解;如何将Redis缓存融入业务逻辑,以减少数据库压力,并举例说明了访问逻辑;同时,针对数据库更新后缓存同步的问题,提出了缓存同步服务的解决方案,强调了在CRUD操作中调用此服务的重要性;最后提到了Redis客户端jedis的使用。
摘要由CSDN通过智能技术生成

关于redis为什么能作为缓存这个问题我们就不说了,直接来说一下redis缓存到底如何在项目中使用吧:

1.redis缓存如何在项目中配置?

   1.1redis缓存单机版和集群版配置?(redis的客户端jedis常用)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.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>  

    <!-- jedis客户端单机版 -->

    <bean id="redisClient" class="redis.clients.jedis.JedisPool">

        <constructor-arg name="host" value="192.168.146.131"></constructor-arg>

        <constructor-arg name="port" value="6379"></constructor-arg>

        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>

    </bean>

    <bean id="jedisClient" class="com.taotao.rest.dao.impl.JedisClientSingle"/>

     

     

    <!-- jedis集群版配置 -->

    <!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster">

        <constructor-arg name="nodes">

            <set>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7001"></constructor-arg>

                </bean>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7002"></constructor-arg>

                </bean>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7003"></constructor-arg>

                </bean>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7004"></constructor-arg>

                </bean>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7005"></constructor-arg>

                </bean>

                <bean class="redis.clients.jedis.HostAndPort">

                    <constructor-arg name="host" value="192.168.25.153"></constructor-arg>

                    <constructor-arg name="port" value="7006"></constructor-arg>

                </bean>

            </set>

        </constructor-arg>

        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>

    </bean>

    <bean id="jedisClientCluster" class="com.taotao.rest.dao.impl.JedisClientCluster"></bean> -->

</beans>

  

   1.2redis的方法定义?

接口:

实现:分集群和单机版

单机版实现方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

package com.taotao.rest.dao.impl;

 

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

 

import com.taotao.rest.dao.JedisClient;

 

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

 

public class JedisClientSingle implements JedisClient{

     

    @Autowired

    private JedisPool jedisPool;

     

    @Override

    public String get(String key) {

        Jedis jedis = jedisPool.getResource();

        String string = jedis.get(key);

        jedis.close();

        return string;

    }

 

    @Override

    public String set(String key, String value) {

        Jedis jedis = jedisPool.getResource();

        String string = jedis.set(key, value);

        jedis.close();

        return string;

    }

 

    @Override

    public String hget(String hkey, String key) {

        Jedis jedis = jedisPool.getResource();

        String string = jedis.hget(hkey, key);

        jedis.close();

        return string;

    }

 

    @Override

    public long hset(String hkey, String key, String value) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.hset(hkey, key, value);

        jedis.close();

        return result;

    }

 

    @Override

    public long incr(String key) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.incr(key);

        jedis.close();

        return result;

    }

 

    @Override

    public long expire(String key, int second) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.expire(key, second);

        jedis.close();

        return result;

    }

 

    @Override

    public long ttl(String key) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.ttl(key);

        jedis.close();

        return result;

    }

 

    @Override

    public long del(String key) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.del(key);

        jedis.close();

        return result;

    }

 

    @Override

    public long hdel(String hkey, String key) {

        Jedis jedis = jedisPool.getResource();

        Long result = jedis.hdel(hkey, key);

        jedis.close();

        return result;

    }

 

}

  集群版的实现方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

package com.taotao.rest.dao.impl;

 

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

 

import com.taotao.rest.dao.JedisClient;

 

import redis.clients.jedis.JedisCluster;

 

public class JedisClientCluster implements JedisClient {

 

    @Autowired

    private JedisCluster jedisCluster;

     

    @Override

    public String get(String key) {

        return jedisCluster.get(key);

    }

 

    @Override

    public String set(String key, String value) {

        return jedisCluster.set(key, value);

    }

 

    @Override

    public String hget(String hkey, String key) {

        return jedisCluster.hget(hkey, key);

    }

 

    @Override

    public long hset(String hkey, String key, String value) {

        return jedisCluster.hset(hkey, key, value);

    }

 

    @Override

    public long incr(String key) {

        return jedisCluster.incr(key);

    }

 

    @Override

    public long expire(String key, int second) {

        return jedisCluster.expire(key, second);

    }

 

    @Override

    public long ttl(String key) {

        return jedisCluster.ttl(key);

    }

 

    @Override

    public long del(String key) {

         

        return jedisCluster.del(key);

    }

 

    @Override

    public long hdel(String hkey, String key) {

         

        return jedisCluster.hdel(hkey, key);

    }

 

}

  配置好后,如何添加到代码中呢?

 

2.redis缓存如何添加到业务逻辑代码中?

redis作为缓存的作用就是减少对数据库的访问压力,当我们访问一个数据的时候,首先我们从redis中查看是否有该数据,如果没有,则从数据库中读取,将从数据库中读取的数据存放到缓存中,下次再访问同样的数据的是,还是先判断redis中是否存在该数据,如果有,则从缓存中读取,不访问数据库了。

举个例子:根据内容分类id访问内容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

package com.taotao.rest.service.impl;

 

import java.util.ArrayList;

import java.util.List;

 

import org.apache.commons.lang3.StringUtils;

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

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

import org.springframework.stereotype.Service;

 

import com.taotao.commonEntity.JsonUtils;

import com.taotao.commonEntity.TaotaoResult;

import com.taotao.mapper.TbContentMapper;

import com.taotao.pojo.TbContent;

import com.taotao.pojo.TbContentExample;

import com.taotao.pojo.TbContentExample.Criteria;

import com.taotao.rest.dao.JedisClient;

import com.taotao.rest.service.ContentService;

 

import redis.clients.jedis.Jedis;

//首页大广告位的获取服务层信息

@Service

public class ContentServiceImpl implements ContentService {

     

    @Value("${CONTENTCATEGORYID}")

    private String CONTENTCATEGORYID;

    @Autowired

    private TbContentMapper contentMapper;

    @Autowired

    private JedisClient jedisClient;

     

    @Override

    public List<TbContent> getContentList(Long categoryId) {

        /*一般第一次访问的时候先从数据库读取数据,然后将数据写入到缓存,再次访问同一内容的时候就从缓存中读取,如果缓存中没有则从数据库中读取

        所以我们添加缓存逻辑的时候,从数据库中将内容读取出来之后,先set入缓存,然后再从缓存中添加读取行为,如果缓存为空则从数据库中进行读取

        */

        //从缓存中获取值

        String getData = jedisClient.hget(CONTENTCATEGORYID, categoryId+"");

        if (!StringUtils.isBlank(getData)) {

            List<TbContent> resultList= JsonUtils.jsonToList(getData, TbContent.class);

            return resultList; 

        }

        TbContentExample example=new TbContentExample();

        Criteria criteria = example.createCriteria();

        criteria.andCategoryIdEqualTo(categoryId);

       List<TbContent> list = contentMapper.selectByExample(example);

       //向缓存中放入值

       String jsonData = JsonUtils.objectToJson(list);

       jedisClient.hset(CONTENTCATEGORYID, categoryId+"",jsonData);

        return list;

    }

 

}

  所以这里就是写逻辑代码的时候,在业务功能处,从缓存中读取-----从db中读取----将数据写入缓存。

3.针对上面出现的问题:

当我们后台数据库中内容修改之后,因为缓存中的内容没有修改,我们访问的时候都是先访问缓存,所以即使数据库中的内容修改了,但是页面的显示还是不会改变的。因为缓存没有更新,所以这就涉及到缓存同步的问题:即数据库修改了内容与缓存中对应的内容同步。

缓存同步的原理:就是将redis中的key进行删除,下次访问的时候,redis中没有改数据,则从DB进行查询,再次更新到redis中。

我们可以写一个缓存同步的服务:

缓存同步除了查询是没有涉及到同步问题,增加删除修改都会涉及到同步问题。

只需要在后台进行CRUD的地方添加调用该缓存同步的服务即可:

 

 

5.redis客户端jedis的使用:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值