好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

集成Spring Redis缓存的实现

这里的缓存主要是用于 service 层的,所以下面的配置,都是针对 service 模块的。

本文来自内部分享,对特殊信息进行了简单处理。

本文都是在以缓存来讲 redis 的使用,实际上 redis 不仅仅用于缓存,本身还是 nosql 数据库,大家可以自己查找学习 redis 的常用场景。

一、添加依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!--缓存-->

<dependency>

  <groupid>org.springframework</groupid>

  <artifactid>spring-context-support</artifactid>

  <version> 4.3 . 14 .release</version>

</dependency>

<!--redis-->

<dependency>

  <groupid>org.springframework.data</groupid>

  <artifactid>spring-data-redis</artifactid>

  <version> 1.8 . 10 .release</version>

</dependency>

<dependency>

  <groupid>org.apache.commons</groupid>

  <artifactid>commons-pool2</artifactid>

  <version> 2.4 . 3 </version>

</dependency>

<dependency>

  <groupid>redis.clients</groupid>

  <artifactid>jedis</artifactid>

  <version> 2.9 . 0 </version>

</dependency>

二、配置

增加 spring-redis.xml 配置文件,内容如下:

?

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

<?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:cache= "http://www.springframework.org/schema/cache"

xmlns:util= "http://www.springframework.org/schema/util"

xsi:schemalocation="http: //www.springframework.org/schema/beans

http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd

http: //www.springframework.org/schema/cache

http: //www.springframework.org/schema/cache/spring-cache.xsd

http: //www.springframework.org/schema/util

http: //www.springframework.org/schema/util/spring-util.xsd">

   <bean id= "jedispoolconfig" class = "redis.clients.jedis.jedispoolconfig" >

     <property name= "maxidle" value= "${redis.pool.maxidle}" />

     <property name= "maxtotal" value= "${redis.pool.maxtotal}" />

     <property name= "maxwaitmillis" value= "${redis.pool.maxwaitmillis}" />

     <property name= "testonborrow" value= "${redis.pool.testonborrow}" />

     <property name= "testonreturn" value= "${redis.pool.testonreturn}" />

   </bean>

   <bean id= "jedisconnectionfactory" class = "org.springframework.data.redis.connection.jedis.jedisconnectionfactory" >

     <property name= "hostname" value= "${redis.master.ip}" />

     <property name= "port" value= "${redis.master.port}" />

     <property name= "poolconfig" ref= "jedispoolconfig" />

   </bean>

   <bean id= "rediskeyserializer" class = "org.springframework.data.redis.serializer.stringredisserializer" />

   <bean id= "redisvalueserializer" class = "org.springframework.data.redis.serializer.jdkserializationredisserializer" />

   <bean id= "redistemplate" class = "org.springframework.data.redis.core.redistemplate" >

     <property name= "connectionfactory" ref= "jedisconnectionfactory" />

     <property name= "keyserializer" ref= "rediskeyserializer" />

     <property name= "hashkeyserializer" ref= "rediskeyserializer" />

     <property name= "valueserializer" ref= "redisvalueserializer" />

     <property name= "hashvalueserializer" ref= "redisvalueserializer" />

   </bean>

   <!--在 redis.properties 配置缓存详细信息-->

   <util:properties id= "redisexpires" location= "classpath*:meta-inf/spring/redis.properties" />

   <bean id= "cachemanager" class = "org.springframework.data.redis.cache.rediscachemanager" >

     <constructor-arg index= "0" ref= "redistemplate" />

     <!--默认缓存 10 分钟-->

     <property name= "defaultexpiration" value= "600" />

     <property name= "useprefix" value= "true" />

     <property name= "expires" ref= "redisexpires" />

   </bean>

   <!--启用 cache 注解-->

   <cache:annotation-driven cache-manager= "cachemanager" proxy-target- class = "true" />

</beans>

在 src/main/resources/ 下面如果没有 meta-inf/spring/ 目录就创建一个,然后增加 redis.properties 配置,示例如下:

?

1

2

3

4

5

6

# 缓存名=有效时间

halfhour= 1800

onehour= 3600

oneday= 86400

websession= 1800

user= 1800

除了上面配置外,在系统的 application.properties 中还需要提供下面几个配置:

?

1

2

3

4

5

6

7

8

9

# redis 连接配置

redis.master.ip= 10.10 . 10.100

redis.master.port= 6379

# redis 连接池配置

redis.pool.maxidle= 200

redis.pool.maxtotal= 1024

redis.pool.maxwaitmillis= 1000

redis.pool.testonborrow= true

redis.pool.testonreturn= true

三、通过注解方式使用缓存

示例中,redis.propreties 配置如下:

?

1

2

3

4

# 数据库定义,缓存 30 天

databasedef= 2592000

# 数据库元数据,缓存 1 小时

databasemeta= 3600

这个示例在数据库服务上配置的,数据库服务中,查询次数远远大于新增、修改、删除的次数,非常适合使用缓存。

1. 缓存数据 @cacheable

?

1

2

3

4

5

@override

@cacheable (value = "databasedef" , key = "'all'" )

public list<databasedefinitionvo> selectall() {

  return databasedefinitiondao.selectallvo();

}

特别注意:所有这些注解中,key 的值是 spel 表达式,必须按照 spel 要求来写。上面这个例子中,直接定义返回值的 key 是 all 字符串,需要加上单引号' 括起来,下面还有其他用法。

在例子中,下面的方法也使用了这个注解:

?

1

2

3

4

5

6

7

8

@override

@cacheable (value = "databasedef" , key = "#id.tostring()" )

public databasedefinition selectbyprimarykey( long id) {

   assert .notnull(id, "数据库 id 不能为空!" );

   databasedefinition definition = databasedefinitiondao.selectbyprimarykey(id);

   assert .notnull(definition, "数据库定义不存在!" );

   return definition;

}

在上面注解中, key 中的 #id 指的是参数中的 id,在 idea 中会有自动提示。 .tostring() 是调用 id 的方法,在系统中规定了 key 必须是字符串类型,所以当类型是 long 的时候,需要转换。

使用缓存的目的就是为了减少上面两个方法调用时减少和数据库的交互,减小数据库的压力,这是两个主要的缓存数据的方法,下面的几个操作都和上面这两个方法有一定的关系。

重点:这里以及下面几个注解中,都指定了 value = "databasedef" ,这里的意思是说,要使用前面配置中的 databasedef 对应的配置,也就是会缓存 30天。

2. 更新缓存 @cacheput

?

1

2

3

4

5

6

7

@override

@cacheput (value = "databasedef" , key = "#result.id.tostring()" )

public databasedefinition save(databasedefinition definition, currentuser usermodel)

    throws serviceexception {

  //代码

  return definition;

}

更新缓存的方法需要注意的是返回值,在上面 save 方法中,有可能是新增,有可能是更新,不管是那个操作,当操作完成后,上面注解会根据 key 的值生成 key,然后将方法的返回值作为 value 保存到缓存中。

这里 key 的写法中 #result 指代返回值,.id 是返回值的属性,.tostring() 是调用 id 属性的方法,在系统中规定了 key 必须是字符串类型,所以当类型是 long 的时候,需要转换。

这个方法上加的缓存还有问题,当新增或者更新后,通过 selectall() 返回的值已经发生了变化,但是这里没有清除 all 的缓存值,会导致 selectall() 出现脏数据,下面会通过 @caching 注解改造这里。

3. 清除缓存 @cacheevict

?

1

2

3

4

5

6

7

8

9

@override

@cacheevict (value = "databasedef" , key = "#id.tostring()" )

public void deletebyprimarykey( long id) throws serviceexception {

  databasedefinition definition = selectbyprimarykey(id);

  if (definition.getloadstate().equals(databasedefinition.loadstate.up)) {

   throw new serviceexception( "请先卸载数据库!" );

  }

  databasedefinitiondao.deletebyprimarykey(id);

}

在上面新增或者修改的时候根据 id 缓存或者更新了缓存数据,这里当删除数据的时候,还需要清空对应的缓存数据。

在上面注解中,key 中的 #id 指的是参数中的 id,在 idea 中会有自动提示。

这个方法上加的缓存还有问题,当删除后,通过 selectall() 返回的值已经发生了变化,但是这里没有清除 all 的缓存值,会导致 selectall() 出现脏数据,下面会通过 @caching 注解改造这里。

4. 组合使用 @caching

上面两个注解中,都提到了脏数据,通过 @caching 注解可以解决这个问题。

先修改第二个注解,来解决 save 时的脏数据:

?

1

2

3

4

5

6

7

8

@override

@caching (put = @cacheput (value = "databasedef" , key = "#result.id.tostring()" ),

      evict = @cacheevict (value = "databasedef" , key = "'all'" ))

public databasedefinition save(databasedefinition definition, currentuser usermodel)

    throws serviceexception {

  //其他代码

  return definition;

}

前面说明,新增或者修改的时候,all 缓存中的数据已经不对了,因此这里在 put 的同时,使用 evict 将 'all' 中的数据清除,这就保证了 selelctall 下次调用时,会重新从库中读取数据。

对上面的删除方法,也进行类似的修改:

?

1

2

3

4

5

6

7

8

9

10

11

12

@override

@caching (evict = {

   @cacheevict (value = "databasedef" , key = "#id.tostring()" ),

   @cacheevict (value = "databasedef" , key = "'all'" )

})

public void deletebyprimarykey( long id) throws serviceexception {

  databasedefinition definition = selectbyprimarykey(id);

  if (definition.getloadstate().equals(databasedefinition.loadstate.up)) {

   throw new serviceexception( "请先卸载数据库!" );

  }

  databasedefinitiondao.deletebyprimarykey(id);

}

注意这里的 evict 是个数组,里面配置了两个清除缓存的配置。

5. 全局配置 @cacheconfig

在上面所有例子中,都指定了 value = "databasedef",实际上可以通过在类上使用 @cacheconfig 注解配置当前类中的 cachenames 值,配置后,如果和类上的 value 一样就不需要在每个注解单独配置。只有不同时再去指定,方法上的 value 值优先级更高。

?

1

2

3

4

@service

@cacheconfig (cachenames = "databasedef" )

public class databasedefinitionserviceimpl implements

      databasedefinitionservice, databasesqlexecuteservice, applicationlistener {

有了上面配置后,其他缓存名字相同的地方可以简化,例如删除方法修改后如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

@override

@caching (evict = {

   @cacheevict (key = "#id.tostring()" ),

   @cacheevict (key = "'all'" )

})

public void deletebyprimarykey( long id) throws serviceexception {

  databasedefinition definition = selectbyprimarykey(id);

  if (definition.getloadstate().equals(databasedefinition.loadstate.up)) {

   throw new serviceexception( "请先卸载数据库!" );

  }

  databasedefinitiondao.deletebyprimarykey(id);

}

其他例子

除了上面针对 databasedef 的缓存外,还有 databasemeta 的配置:

?

1

2

3

4

5

6

7

8

9

10

11

12

@override

@cacheable (value = "databasemeta" , key = "#databaseid.tostring()" )

public list<tablevo> selecttablesbydatabaseid( long databaseid)

   throws exception {

  //代码

}

@override

@cacheable (value = "databasemeta" , key = "#databaseid + '_' + #tablename" )

public tablevo selecttablebydatabaseidandtablename( long databaseid, string tablename)

   throws exception {

  //代码

}

这两个方法是获取数据库元数据的,只有修改数据库表的时候才会变化,因此不存在清除缓存的情况,但是万一修改表后,想要新的元数据,该怎么办?

因此增加了一个空的方法来清空数据,方法如下:

?

1

2

3

4

@override

@cacheevict (value = "databasemeta" , allentries = true )

public void cleantablescache() {

}

这里指定了 databasemeta,通过 allentries = true 清空所有 key 的缓存。通过这个方法可以保证在有需要的时候清空所有元数据的缓存。

实际上如果想要更精确的清除,可以传入要清除的 databaseid 和 tablename 来更精确的清除。

增加缓存后的效果

调用 selecttablesbydatabaseid 多次时,输出的日志如下:

info  c.n.d.u.dynamicdatasource - 当前数据源:默认数据源
debug c.n.d.datascopecontextproviderfilter - 服务调用返回前清除数据权限信息
info  c.n.d.u.dynamicdatasource - 当前数据源:默认数据源
debug c.n.d.d.d.selectbyprimarykey - ==>  preparing: select xxx (隐藏完整 sql)
debug c.n.d.d.d.selectbyprimarykey - ==> parameters: 1(long)
debug c.n.d.d.d.selectbyprimarykey - <==      total: 1
info  c.n.d.u.dynamicdatasource - 当前数据源:10.10.10.130/datareporting
debug c.n.d.datascopecontextproviderfilter - 服务调用返回前清除数据权限信息
info  c.n.d.u.dynamicdatasource - 当前数据源:默认数据源
debug c.n.d.datascopecontextproviderfilter - 服务调用返回前清除数据权限信息
info  c.n.d.u.dynamicdatasource - 当前数据源:默认数据源
debug c.n.d.datascopecontextproviderfilter - 服务调用返回前清除数据权限信息

从日志可以看出来,只有第一次进行了数据库查询,后续通过日志看不到数据库操作。

调用清空缓存后,会再次查询数据库。

初次调用时,web请求花了 700多ms,后面再次调用时,平均不到 30 ms,这就是缓存最明显的作用。

连接到 redis 服务后,查看所有 key,结果如下:

redis@redissvr:~$ redis-cli 
127.0.0.1:6379> keys *
1) "databasemeta:1"
2) "databasedef:all"
127.0.0.1:6379>

缓存中的数据都有 value 前缀,上面缓存了 all 和 id 为 1 的数据。

缓存注解是一种最简单的缓存方式,但是需要配合 value 属性的配置来使用,许多时候我们可能需要更精确的控制缓存,此时可以使用 redistemplate 来控制。

四、通过 redistemplate 使用缓存

有关这部分的详细用法可以从网上搜索相关内容进行学习,这里列举一个简单的例子。

针对前面的 selectall 我们换一种方式进行缓存。

首先注入下面的接口:

?

1

2

@resource (name = "redistemplate" )

private valueoperations<string, list> valueoper;

修改 selectall 方法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

@override

//@cacheable(key = "'all'")

public list<databasedefinitionvo> selectall() {

  list<databasedefinitionvo> vos = valueoper.get( "databasedef:all" );

  if (vos != null ){

   return vos;

  }

  vos = databasedefinitiondao.selectallvo();

  //缓存 1 小时

  valueoper.set( "databasedef:all" , vos, 1 , timeunit.hours);

  return vos;

}

首先通过 valueoper.get("databasedef:all") 尝试获取缓存信息,如果存在就直接返回。

如果不存在,就查询数据库,然后将查询结果通过 set 进行缓存。

特别注意: 上面的 key,写的是 "databasedef:all",也就是前缀需要自己加上,如果直接写成 all,在 redis 中的 key 就是 all,不会自动增加前缀。

如果没有前缀,那么当不同系统都使用 all 时,数据就会混乱!

五、redis 服务器配置注意事项

内网使用的服务器,特殊配置如下:

?

1

2

3

4

5

6

# by default protected mode is enabled. you should disable it only if

# you are sure you want clients from other hosts to connect to redis

# even if no authentication is configured, nor a specific set of interfaces

# are explicitly listed using the "bind" directive.

# protected -mode yes

protected -mode no

关闭了保护模式。

?

1

2

3

4

# if you are sure you want your instance to listen to all the interfaces

# just comment the following line.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# bind 127.0 . 0.1

注释了绑定的 ip,这样可以让所有电脑访问 redis。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/isea533/article/details/84563949

查看更多关于集成Spring Redis缓存的实现的详细内容...

  阅读:13次