缓存(本地缓存)

在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计。

  • 常用的缓存分两种:本地缓存和分布式缓存。
  • 常用的本地缓存是guava cache,本文主要介绍guava cache在项目中的使用,首先来了解下为什么使用缓存,以数据库(如MYSQL)、本地缓存(如guava cache)及分布式缓存(如redis)的区别来讲:

一、数据库、本地缓存及分布式缓存的区别

1、存储位置:

  • 数据库:任何机器硬盘,开关机时,数据不丢失
  • 本地缓存:本机内存,一旦关机,之前的数据丢失
  • 分布式缓存(如redis):存在redis所在机器的内存,一旦redis所在机器关机,数据丢失

2、持久化:

  • 数据库:可以持久化
  • 本地缓存:不可以持久化
  • 分布式缓存(如redis):不可以持久化

3、访问速度:

  • 数据库:慢
  • 本地缓存:最快
  • 分布式缓存:快

4、扩展性:

  • 数据库:可扩展,可存在其他机器的硬盘
  • 本地缓存:不可扩展,只能存在本机内存
  • 分布式缓存:可扩展,可存在其他机器的内存

5、使用场景:

  • 数据库:需要实现持久化保存
  • 本地缓存:需要快速访问,但需要考虑内存大小
  • 分布式缓存:1)需要快速访问,不需要考虑内存大小

                         2)需要实现持久化,但会丢失一些数据

                         3)需要让缓存集中在一起,访问任一机器上内存中的数据都可以从缓存中得到

二、实例

     基于SSMM框架整合步骤,连接(SSMM(spring+springmvc+maven+mybatis)框架整合)的flowershop项目。

1、启动MYSQL

2、创建mvn项目

  • 配置pom.xml文件(这里直接给出已经配置好的)
  •   1 <?xml version="1.0" encoding="UTF-8"?>
      2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      4 
      5     <modelVersion>4.0.0</modelVersion>
      6 
      7     <groupId>com.wn</groupId>
      8     <artifactId>flowershop</artifactId>
      9     <version>1.0-SNAPSHOT</version>
     10     <name>flowershop</name>
     11     <packaging>war</packaging>
     12     <properties>
     13         <java.version>1.8</java.version>
     14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     15         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     16         <spring.version>4.2.4.RELEASE</spring.version>
     17         <mybatis.version>3.2.8</mybatis.version>
     18         <mybatis-spring.version>1.2.2</mybatis-spring.version>
     19         <jackson.version>2.6.4</jackson.version>
     20         <servlet-api.version>3.1.0</servlet-api.version>
     21     </properties>
     22     <dependencies>
     23         <!-- servlet -->
     24         <dependency>
     25             <groupId>javax.servlet</groupId>
     26             <artifactId>javax.servlet-api</artifactId>
     27             <version>${servlet-api.version}</version>
     28             <scope>provided</scope>
     29         </dependency>
     30         <!-- 引入spring -->
     31         <dependency>
     32             <groupId>org.springframework</groupId>
     33             <artifactId>spring-core</artifactId>
     34             <version>${spring.version}</version>
     35         </dependency>
     36         <dependency>
     37             <groupId>org.springframework</groupId>
     38             <artifactId>spring-beans</artifactId>
     39             <version>${spring.version}</version>
     40         </dependency>
     41         <dependency>
     42             <groupId>org.springframework</groupId>
     43             <artifactId>spring-context</artifactId>
     44             <version>${spring.version}</version>
     45         </dependency>
     46         <!-- spring mvc -->
     47         <dependency>
     48             <groupId>org.springframework</groupId>
     49             <artifactId>spring-web</artifactId>
     50             <version>${spring.version}</version>
     51         </dependency>
     52         <dependency>
     53             <groupId>org.springframework</groupId>
     54             <artifactId>spring-webmvc</artifactId>
     55             <version>${spring.version}</version>
     56         </dependency>
     57         <!-- 数据库相关 -->
     58         <dependency>
     59             <groupId>org.springframework</groupId>
     60             <artifactId>spring-jdbc</artifactId>
     61             <version>${spring.version}</version>
     62         </dependency>
     63         <!-- java连mysql必须的包 -->
     64         <dependency>
     65             <groupId>mysql</groupId>
     66             <artifactId>mysql-connector-java</artifactId>
     67             <version>5.1.27</version>
     68             <scope>runtime</scope>
     69         </dependency>
     70         <!-- 数据源 -->
     71         <dependency>
     72             <groupId>org.apache.tomcat</groupId>
     73             <artifactId>tomcat-jdbc</artifactId>
     74             <version>7.0.47</version>
     75         </dependency>
     76         <!-- 引入mybatis -->
     77         <dependency>
     78             <groupId>org.mybatis</groupId>
     79             <artifactId>mybatis</artifactId>
     80             <version>${mybatis.version}</version>
     81         </dependency>
     82         <!-- mybatis与spring进行集成的包 -->
     83         <dependency>
     84             <groupId>org.mybatis</groupId>
     85             <artifactId>mybatis-spring</artifactId>
     86             <version>${mybatis-spring.version}</version>
     87         </dependency>
     88         <!-- json -->
     89         <dependency>
     90             <groupId>com.fasterxml.jackson.core</groupId>
     91             <artifactId>jackson-annotations</artifactId>
     92             <version>${jackson.version}</version>
     93         </dependency>
     94         <dependency>
     95             <groupId>com.fasterxml.jackson.core</groupId>
     96             <artifactId>jackson-core</artifactId>
     97             <version>${jackson.version}</version>
     98         </dependency>
     99         <dependency>
    100             <groupId>com.fasterxml.jackson.core</groupId>
    101             <artifactId>jackson-databind</artifactId>
    102             <version>${jackson.version}</version>
    103         </dependency>
    104         <!-- guwa cache -->
    105         <dependency>
    106             <groupId>com.google.guava</groupId>
    107             <artifactId>guava</artifactId>
    108             <version>14.0.1</version>
    109         </dependency>
    110     </dependencies>
    111     <build>
    112         <plugins>
    113             <plugin>
    114                 <groupId>org.apache.maven.plugins</groupId>
    115                 <artifactId>maven-compiler-plugin</artifactId>
    116                 <configuration>
    117                     <source>1.8</source>
    118                     <target>1.8</target>
    119                     <encoding>utf-8</encoding>
    120                 </configuration>
    121             </plugin>
    122         </plugins>
    123     </build>
    124 </project>
    View Code

    这里用到guava cache,所以别忘了引入依赖guava cache

  • 1               <!-- guwa cache -->
    2         <dependency>
    3             <groupId>com.google.guava</groupId>
    4             <artifactId>guava</artifactId>
    5             <version>14.0.1</version>
    6         </dependency>
  •  引入mysql库配置(src/main/resources)

    jdbc.properties

1 jdbc.driverClassName = com.mysql.jdbc.Driver
2 jdbc.url = jdbc:mysql://localhost:3306/wn
3 jdbc.username = root
4 jdbc.password = 123456
  • 引入spring配置文件(src/main/resources)

        spring.xml

  •  1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:mvc="http://www.springframework.org/schema/mvc"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     6                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     7                            http://www.springframework.org/schema/context 
     8                            http://www.springframework.org/schema/context/spring-context-3.2.xsd
     9                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    10 
    11     <!-- 注解扫描 -->
    12     <context:component-scan base-package="com.wn" />
    13     <!-- 启动jackson -->
    14     <mvc:annotation-driven />
    15     <!-- 引入属性文件 -->
    16     <context:property-placeholder location="classpath:jdbc.properties" />
    17 
    18     <!-- 引入数据源 -->
    19     <bean id="flowerDataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    20         destroy-method="close">
    21         <property name="driverClassName" value="${jdbc.driverClassName}" />
    22         <property name="url" value="${jdbc.url}" />
    23         <property name="username" value="${jdbc.username}" />
    24         <property name="password" value="${jdbc.password}" />
    25     </bean>
    26 
    27     <!-- 引入mybatis -->
    28     <bean id="flowerSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    29         <property name="dataSource" ref="flowerDataSource" />
    30         <property name="mapperLocations">
    31             <list>
    32                 <value>classpath*:mapper/*Mapper.xml</value>
    33             </list>
    34         </property>
    35     </bean>
    36     <bean id="flowerMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    37         <property name="basePackage" value="com.wn.mapper" />
    38         <property name="sqlSessionFactoryBeanName" value="flowerSqlSessionFactory" />
    39     </bean>
    40 </beans>
    View Code
  • 创建基本包(package)结构(controller、service、dao、mapper、model)

  • 创建src/main/resources/mapper的folder包,用于放置*Mapper.xml
  • 配置web.xml,使spring生效

  •  1 <?xml version="1.0" encoding="utf-8"?>
     2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     5     <servlet>
     6         <servlet-name>dispatcherServlet</servlet-name>
     7         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     8         <init-param>
     9             <param-name>contextConfigLocation</param-name>
    10             <param-value>classpath*:spring*.xml</param-value>
    11         </init-param>
    12         <load-on-startup>1</load-on-startup>
    13     </servlet>
    14     <servlet-mapping>
    15         <servlet-name>dispatcherServlet</servlet-name>
    16         <url-pattern>/</url-pattern>
    17     </servlet-mapping>
    18 
    19     <filter>
    20         <filter-name>encodingFilter</filter-name>
    21         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    22         <init-param>
    23             <param-name>encoding</param-name>
    24             <param-value>UTF-8</param-value>
    25         </init-param>
    26         <init-param>
    27             <param-name>forceEncoding</param-name>
    28             <param-value>true</param-value>
    29         </init-param>
    30     </filter>
    31     <filter-mapping>
    32         <filter-name>encodingFilter</filter-name>
    33         <url-pattern>/*</url-pattern>
    34     </filter-mapping>
    35    </web-app>
    View Code

3使用mybatis-generator创建mybatis的xml文件、mapper层接口、model层

  • 将生成的类、接口、xml拷贝到项目中
  • FlowerMapper.xml
  •   1 <?xml version="1.0" encoding="UTF-8" ?>
      2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
      3 <mapper namespace="com.wn.mapper.FlowerMapper">
      4     <resultMap id="BaseResultMap" type="com.wn.model.Flower">
      5         <id column="id" property="id" jdbcType="INTEGER" />
      6         <result column="type" property="type" jdbcType="VARCHAR" />
      7         <result column="colour" property="colour" jdbcType="VARCHAR" />
      8         <result column="number" property="number" jdbcType="INTEGER" />
      9         <result column="price" property="price" jdbcType="DECIMAL" />
     10         <result column="florescence" property="florescence" jdbcType="INTEGER" />
     11     </resultMap>
     12     <sql id="Base_Column_List">
     13         id, type, colour, number, price, florescence
     14     </sql>
     15     <select id="selectByPrimaryKey" resultMap="BaseResultMap"
     16         parameterType="java.lang.Integer">
     17         select
     18         <include refid="Base_Column_List" />
     19         from t_flower
     20         where id = #{id,jdbcType=INTEGER}
     21     </select>
     22     <select id="selectByColour" resultMap="BaseResultMap"
     23         parameterType="java.lang.String">
     24         select
     25         <include refid="Base_Column_List" />
     26         from t_flower
     27         where colour=#{colour,jdbcType=VARCHAR}
     28     </select>
     29               <!-- 实现条件不定查询 -->
     30     <select id="selectByCondition" resultMap="BaseResultMap"
     31         parameterType="java.lang.Integer">
     32         select
     33         <include refid="Base_Column_List" />
     34         from t_flower
     35         <where>
     36             <if test="type!=null">
     37                 type=#{type,jdbcType=VARCHAR}
     38             </if>
     39             <if test="colour!=null">
     40                 AND colour=#{colour,jdbcType=VARCHAR}
     41             </if>
     42             <if test="number!=null">
     43                 AND number=#{number,jdbcType=INTEGER}
     44             </if>
     45             <if test="price!=null">
     46                 AND price=#{price,jdbcType=DECIMAL}
     47             </if>
     48             <if test="florescence!=null">
     49                 AND florescence=#{florescence,jdbcType=INTEGER}
     50             </if>
     51         </where>
     52         <!-- 按照单价降序排列,并实现分页展示功能 -->
     53     </select>
     54     <select id="selectLimit" resultMap="BaseResultMap"
     55         parameterType="java.lang.Integer">
     56         select
     57         <include refid="Base_Column_List" />
     58         from t_flower
     59         order by price DESC limit #{start}, #{size}
     60     </select>
     61     <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
     62         delete from
     63         t_flower
     64         where id = #{id,jdbcType=INTEGER}
     65     </delete>
     66     <insert id="insert" parameterType="com.wn.model.Flower">
     67         insert into t_flower (id,
     68         type, colour,
     69         number, price, florescence
     70         )
     71         values
     72         (#{id,jdbcType=INTEGER}, #{type,jdbcType=VARCHAR},
     73         #{colour,jdbcType=VARCHAR},
     74         #{number,jdbcType=INTEGER},
     75         #{price,jdbcType=DECIMAL}, #{florescence,jdbcType=INTEGER}
     76         )
     77     </insert>
     78     <insert id="insertSelective" parameterType="com.wn.model.Flower">
     79         insert into t_flower
     80         <trim prefix="(" suffix=")" suffixOverrides=",">
     81             <if test="id != null">
     82                 id,
     83             </if>
     84             <if test="type != null">
     85                 type,
     86             </if>
     87             <if test="colour != null">
     88                 colour,
     89             </if>
     90             <if test="number != null">
     91                 number,
     92             </if>
     93             <if test="price != null">
     94                 price,
     95             </if>
     96             <if test="florescence != null">
     97                 florescence,
     98             </if>
     99         </trim>
    100         <trim prefix="values (" suffix=")" suffixOverrides=",">
    101             <if test="id != null">
    102                 #{id,jdbcType=INTEGER},
    103             </if>
    104             <if test="type != null">
    105                 #{type,jdbcType=VARCHAR},
    106             </if>
    107             <if test="colour != null">
    108                 #{colour,jdbcType=VARCHAR},
    109             </if>
    110             <if test="number != null">
    111                 #{number,jdbcType=INTEGER},
    112             </if>
    113             <if test="price != null">
    114                 #{price,jdbcType=DECIMAL},
    115             </if>
    116             <if test="florescence != null">
    117                 #{florescence,jdbcType=INTEGER},
    118             </if>
    119         </trim>
    120     </insert>
    121     <update id="updateByPrimaryKeySelective" parameterType="com.wn.model.Flower">
    122         update t_flower
    123         <set>
    124             <if test="type != null">
    125                 type = #{type,jdbcType=VARCHAR},
    126             </if>
    127             <if test="colour != null">
    128                 colour = #{colour,jdbcType=VARCHAR},
    129             </if>
    130             <if test="number != null">
    131                 number = #{number,jdbcType=INTEGER},
    132             </if>
    133             <if test="price != null">
    134                 price = #{price,jdbcType=DECIMAL},
    135             </if>
    136             <if test="florescence != null">
    137                 florescence = #{florescence,jdbcType=INTEGER},
    138             </if>
    139         </set>
    140         where id = #{id,jdbcType=INTEGER}
    141     </update>
    142     <update id="updateByPrimaryKey" parameterType="com.wn.model.Flower">
    143         update t_flower
    144         set type = #{type,jdbcType=VARCHAR},
    145         colour =
    146         #{colour,jdbcType=VARCHAR},
    147         number = #{number,jdbcType=INTEGER},
    148         price =
    149         #{price,jdbcType=DECIMAL},
    150         florescence =
    151         #{florescence,jdbcType=INTEGER}
    152         where id = #{id,jdbcType=INTEGER}
    153     </update>
    154 </mapper>
    View Code
  • Flower
  •  1 package com.wn.model;
     2 
     3 public class Flower {
     4     private Integer id;
     5 
     6     private String type;
     7 
     8     private String colour;
     9 
    10     private Integer number;
    11 
    12     private Long price;
    13 
    14     private Integer florescence;
    15 
    16     public Integer getId() {
    17         return id;
    18     }
    19 
    20     public void setId(Integer id) {
    21         this.id = id;
    22     }
    23 
    24     public String getType() {
    25         return type;
    26     }
    27 
    28     public void setType(String type) {
    29         this.type = type == null ? null : type.trim();
    30     }
    31 
    32     public String getColour() {
    33         return colour;
    34     }
    35 
    36     public void setColour(String colour) {
    37         this.colour = colour == null ? null : colour.trim();
    38     }
    39 
    40     public Integer getNumber() {
    41         return number;
    42     }
    43 
    44     public void setNumber(Integer number) {
    45         this.number = number;
    46     }
    47 
    48     public Long getPrice() {
    49         return price;
    50     }
    51 
    52     public void setPrice(Long price) {
    53         this.price = price;
    54     }
    55 
    56     public Integer getFlorescence() {
    57         return florescence;
    58     }
    59 
    60     public void setFlorescence(Integer florescence) {
    61         this.florescence = florescence;
    62     }
    63 }
    View Code
  • FlowerMapper.java
  •  1 package com.wn.mapper;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.annotations.Param;
     6 
     7 import com.wn.model.Flower;
     8 
     9 public interface FlowerMapper {
    10     int deleteByPrimaryKey(Integer id);
    11 
    12     int insert(Flower record);
    13 
    14     int insertSelective(Flower record);
    15 
    16     Flower selectByPrimaryKey(Integer id);
    17 
    18     List<Flower> selectByCondition(Flower record);
    19     
    20     List<Flower> selectLimit(@Param("start") int start, @Param("size") int size);
    21 
    22     int updateByPrimaryKeySelective(Flower record);
    23 
    24     int updateByPrimaryKey(Flower record);
    25     
    26     List<Flower> selectByColour(String colour);
    27 }
    View Code

4、编写dao、service、controller

  • FlowerDao
  •  1 package com.wn.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Repository;
     7 
     8 import com.wn.mapper.FlowerMapper;
     9 import com.wn.model.Flower;
    10 
    11 @Repository
    12 public class FlowerDao {
    13     @Autowired
    14     private FlowerMapper flowerMapper;
    15 
    16     public boolean add(Flower flower) {
    17         return flowerMapper.insert(flower) == 1 ? true : false;
    18     }
    19 
    20     public Flower getById(Integer id) {
    21         return flowerMapper.selectByPrimaryKey(id);
    22     }
    23 
    24     public boolean deleteById(Integer id) {
    25         return flowerMapper.deleteByPrimaryKey(id) == 1 ? true : false;
    26     }
    27 
    28     public boolean update(Flower flower) {
    29         return flowerMapper.updateByPrimaryKey(flower) == 1 ? true : false;
    30     }
    31 
    32     public boolean update2(Flower flower) {
    33         return flowerMapper.updateByPrimaryKeySelective(flower) == 1 ? true : false;
    34     }
    35 
    36     /*
    37      * 实现条件不定查询
    38      */
    39     public List<Flower> getByCondition(Flower flower) {
    40         return flowerMapper.selectByCondition(flower);
    41     }
    42 
    43     /*
    44      * 传入开始显示记录的索引,即显示记录的条数,实现分页显示功能
    45      */
    46     public List<Flower> selectLimit(int start, int size) {
    47         return flowerMapper.selectLimit(start, size);
    48     }
    49 
    50     public List<Flower> selectByColour(String colour) {
    51         return flowerMapper.selectByColour(colour);
    52     }
    53 }
    View Code
  • FlowerService
  •  1 package com.wn.service;
     2 
     3 import java.util.List;
     4 import java.util.concurrent.ExecutionException;
     5 import java.util.concurrent.TimeUnit;
     6 
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.stereotype.Service;
     9 
    10 import com.google.common.cache.CacheBuilder;
    11 import com.google.common.cache.CacheLoader;
    12 import com.google.common.cache.LoadingCache;
    13 import com.wn.dao.FlowerDao;
    14 import com.wn.model.Flower;
    15 
    16 @Service
    17 public class FlowerService {
    18     @Autowired
    19     private FlowerDao flowerDao;
    20 
    21     public boolean addFlower(String type, String colour, Integer number, long price, Integer florescence) {
    22         Flower flower = new Flower();
    23         flower.setType(type);
    24         flower.setColour(colour);
    25         flower.setNumber(number);
    26         flower.setPrice(price);
    27         flower.setFlorescence(florescence);
    28         return flowerDao.add(flower);
    29     }
    30 
    31     public Flower getFlower(Integer id) {
    32         return flowerDao.getById(id);
    33     }
    34 
    35     public boolean deleteFlower(Integer id) {
    36         return flowerDao.deleteById(id);
    37     }
    38 
    39     public boolean updateFlower(Integer id, String type, String colour, Integer number, Long price,
    40             Integer florescence) {
    41         Flower flower = flowerDao.getById(id);
    42         flower.setType(type);
    43         flower.setColour(colour);
    44         flower.setNumber(number);
    45         flower.setPrice(price);
    46         flower.setFlorescence(florescence);
    47         return flowerDao.update(flower);
    48     }
    49 
    50     public boolean updateFlower2(Integer id, String type, String colour, Integer number, Long price,
    51             Integer florescence) {
    52         Flower flower = flowerDao.getById(id);
    53         flower.setType(type);
    54         flower.setColour(colour);
    55         flower.setNumber(number);
    56         flower.setPrice(price);
    57         flower.setFlorescence(florescence);
    58         return flowerDao.update2(flower);
    59     }
    60 
    61     public List<Flower> getFlowerByCondition(String type, String colour, Integer number, Long price,
    62             Integer florescence) {
    63         Flower flower = new Flower();
    64         flower.setColour(colour);
    65         flower.setType(type);
    66         flower.setNumber(number);
    67         flower.setPrice(price);
    68         flower.setFlorescence(florescence);
    69         return flowerDao.getByCondition(flower);
    70     }
    71 
    72     public List<Flower> selectLimit(Integer start, Integer size) {
    73         return flowerDao.selectLimit(start, size);
    74     }
    75 
    76     /**本地缓存-guava cache的流程:
    77      * 1、从guava cache中获取 -- flowerCache.get(colour) 
    78      * 2、如果存在,直接返回数据给controller
    79      * 3、如果不存在,调用load方法(先从mysql获取,再存入guava cache,最后从guava
    80      * cache读出该数据,返回给controller)
    81      */
    82     public List<Flower> getFlowerWithCache(String colour) {
    83         List<Flower> flowerList = null;
    84         try {
    85             flowerList = flowerCache.get(colour);
    86         } catch (ExecutionException e) {
    87             e.printStackTrace();
    88         }
    89         return flowerList;
    90     }
    91     LoadingCache<String, List<Flower>> flowerCache = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MINUTES)
    92             .maximumSize(200).build(new CacheLoader<String, List<Flower>>() {
    93                 public List<Flower> load(String colour) {
    94                     System.out.println("从数据库查");
    95                     return flowerDao.selectByColour(colour);
    96                 }
    97             });
    98 }
    View Code 
  • FlowerController
  •  1 package com.wn.controller;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.web.bind.annotation.PathVariable;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestMethod;
     9 import org.springframework.web.bind.annotation.RequestParam;
    10 import org.springframework.web.bind.annotation.RestController;
    11 
    12 import com.wn.model.Flower;
    13 import com.wn.service.FlowerService;
    14 
    15 @RestController
    16 @RequestMapping("/flower")
    17 public class FlowerController {
    18     @Autowired
    19     private FlowerService flowerService;
    20 
    21     @RequestMapping(value = "/add", method = RequestMethod.POST)
    22     public boolean addFlower(@RequestParam("type") String type, @RequestParam("colour") String colour,
    23             @RequestParam("number") Integer number, @RequestParam("price") Long price,
    24             @RequestParam("florescence") Integer florescence) {
    25         return flowerService.addFlower(type, colour, number, price, florescence);
    26     }
    27 
    28     @RequestMapping(value = "/get", method = RequestMethod.GET)
    29     public Flower getFlower(Integer id) {
    30         return flowerService.getFlower(id);
    31     }
    32 
    33     @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    34     public boolean deleteFlower(@PathVariable("id") Integer id) {
    35         return flowerService.deleteFlower(id);
    36     }
    37 
    38     @RequestMapping(value = "/update", method = RequestMethod.PUT)
    39     public boolean updateFlower(@RequestParam(value = "id") Integer id,
    40             @RequestParam(value = "type", required = false) String type,
    41             @RequestParam(value = "colour", required = false) String colour,
    42             @RequestParam(value = "number", required = false) Integer number,
    43             @RequestParam(value = "price", required = false) Long price,
    44             @RequestParam(value = "florescence", required = false) Integer florescence) {
    45 
    46         return flowerService.updateFlower(id, type, colour, number, price, florescence);
    47     }
    48 
    49     @RequestMapping(value = "/update2", method = RequestMethod.PUT)
    50     public boolean updateFlower2(@RequestParam(value = "id") Integer id,
    51             @RequestParam(value = "type", required = false) String type,
    52             @RequestParam(value = "colour", required = false) String colour,
    53             @RequestParam(value = "number", required = false) Integer number, @RequestParam(value = "price") Long price,
    54             @RequestParam(value = "florescence", required = false) Integer florescence) {
    55         return flowerService.updateFlower2(id, type, colour, number, price, florescence);
    56     }
    57 
    58     @RequestMapping(value = "/getFlowerByConditon", method = RequestMethod.GET)
    59     public List<Flower> getFlowerByCondition(@RequestParam(value = "type", required = false) String type,
    60             @RequestParam(value = "colour", required = false) String colour,
    61             @RequestParam(value = "number", required = false) Integer number,
    62             @RequestParam(value = "price", required = false) Long price,
    63             @RequestParam(value = "florescence", required = false) Integer florescence) {
    64         return flowerService.getFlowerByCondition(type, colour, number, price, florescence);
    65     }
    66 
    67     @RequestMapping(value = "/getFlowerByLimit", method = RequestMethod.GET)
    68     public List<Flower> getFlowerByLimit(@RequestParam("start") Integer start, @RequestParam("size") Integer size) {
    69         return flowerService.selectLimit(start, size);
    70     }
    71 
    72     // 从本地缓存获取数据,若本地缓存没有,再从数据库获取
    73     @RequestMapping(value = "/getWithCache", method = RequestMethod.GET)
    74     public List<Flower> getWithCache(@RequestParam("colour") String colour) {
    75         return flowerService.getFlowerWithCache(colour);
    76     }
    77 }
    View Code

     

posted @ 2016-11-20 11:38  王小霞  阅读(4019)  评论(0编辑  收藏  举报