1. 程式人生 > >spring mvc配置redis快取

spring mvc配置redis快取

在applicationContext.xml中配置:

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <property name="maxIdle" value="300" />
   <property name="maxActive" value="600" />
   <property name="maxWait" value="1000" />
   <property name="testOnBorrow" value="true" />
</bean>

<bean id
="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="127.0.0.1" p:port="6379" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name
="connectionFactory" ref="connectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"
/> </property> </bean>

建立redisUtil:

package com.functions.meta.ui.model.redis;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.*;
/**
 * Created by Administrator on 2017/1/11 0011.
 */

@Service
public class RedisCacheUtil {
    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;

    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public boolean ping() {
        boolean flag = false;
        try {
            String result = redisTemplate.getConnectionFactory().getConnection().ping();
            if (result.equals("PONG")) {
                flag = true;
            }
        } catch (Exception e){
            flag = false;
        }
        return flag;
    }

    /**
     * 快取基本的物件,Integer、String、實體類等
     * @param key 快取的鍵值
     * @param value 快取的值
     * @return  快取的物件
     */
    public <T> ValueOperations<String,T> setCacheObject(String key, T value)
    {

        ValueOperations<String,T> operation = redisTemplate.opsForValue();
        operation.set(key,value);
        return operation;
    }

    /**
     * 獲得快取的基本物件。
     * @param key  快取鍵值
     * @return   快取鍵值對應的資料
     */
    public <T> T getCacheObject(String key)
    {
        ValueOperations<String,T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    /**
     * 快取List資料
     * @param key  快取的鍵值
     * @param dataList 待快取的List資料
     * @return   快取的物件
     */
    public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList)
    {
        ListOperations listOperation = redisTemplate.opsForList();
        if(null != dataList)
        {
            int size = dataList.size();
            for(int i = 0; i < size ; i ++)
            {

                listOperation.rightPush(key,dataList.get(i));
            }
        }

        return listOperation;
    }

    /**
     * 獲得快取的list物件
     * @param key 快取的鍵值
     * @return  快取鍵值對應的資料
     */
    public <T> List<T> getCacheList(String key)
    {
        List<T> dataList = new ArrayList<T>();
        ListOperations<String,T> listOperation = redisTemplate.opsForList();
        Long size = listOperation.size(key);

        for(int i = 0 ; i < size ; i ++)
        {
            dataList.add((T) listOperation.leftPop(key));
        }

        return dataList;
    }

    /**
     * 快取Set
     * @param key  快取鍵值
     * @param dataSet 快取的資料
     * @return   快取資料的物件
     */
    public <T> BoundSetOperations<String,T> setCacheSet(String key, Set<T> dataSet)
    {
        BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);
  /*T[] t = (T[]) dataSet.toArray();
    setOperation.add(t);*/

        Iterator<T> it = dataSet.iterator();
        while(it.hasNext())
        {
            setOperation.add(it.next());
        }

        return setOperation;
    }

    /**
     * 獲得快取的set
     * @param key
     * @return
     */
    public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/)
    {
        Set<T> dataSet = new HashSet<T>();
        BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key);

        Long size = operation.size();
        for(int i = 0 ; i < size ; i++)
        {
            dataSet.add(operation.pop());
        }
        return dataSet;
    }

    /**
     * 快取Map
     * @param key
     * @param dataMap
     * @return
     */
    public <T> HashOperations<String,String,T> setCacheMap(String key, Map<String,T> dataMap)
    {

        HashOperations hashOperations = redisTemplate.opsForHash();
        if(null != dataMap)
        {

            for (Map.Entry<String, T> entry : dataMap.entrySet()) {

    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
                hashOperations.put(key,entry.getKey(),entry.getValue());
            }

        }

        return hashOperations;
    }

    /**
     * 獲得快取的Map
     * @param key
     * @return
     */
    public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/)
    {
        Map<String, T> map = redisTemplate.opsForHash().entries(key);
  /*Map<String, T> map = hashOperation.entries(key);*/
        return map;
    }

    /**
     * 快取Map
     * @param key
     * @param dataMap
     * @return
     */
    public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap)
    {
        HashOperations hashOperations = redisTemplate.opsForHash();
        if(null != dataMap)
        {

            for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {

    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
                hashOperations.put(key,entry.getKey(),entry.getValue());
            }

        }

        return hashOperations;
    }

    /**
     * 獲得快取的Map
     * @param key
     * @return
     */
    public <T> Map<Integer,T> getCacheIntegerMap(String key/*,HashOperations<String,String,T> hashOperation*/)
    {
        Map<Integer, T> map = redisTemplate.opsForHash().entries(key);
  /*Map<String, T> map = hashOperation.entries(key);*/
        return map;
    }
}

相關推薦

spring mvc配置redis快取

在applicationContext.xml中配置: <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxId

spring boot2配置redis快取快取使用Jackson2序列化

pom配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-b

spring boot 2.x 配置redis快取的注意之兩點(pool連線池以及CacheManager)

1、在SpringBoot的application.yml配置檔案中配置redis資料庫的相關資訊,這裡改動主要有兩點,其一是時間相關的屬性,如spring.redis.timeout,在1.0中,時間

Spring MVC 配置

rri splay sep static type gets source handle webpack 1, RootConfig用來配置ContextLoadListener 1 @Configuration 2 //@Import(DataConf

spring mvc 配置 mybatis sql攔截器

mybatis直接上代碼:mybatis配置中 添加 <property name="plugins"> 如下: <bean id="sqlSessionFactory" class="com.hotent.core.mybatis.SqlSessionFactoryFactoryBea

Spring mvc配置文件

3.2 index springmvc 完成 not frame spa parameter rect SpringMVC是一個基於DispatcherServlet的MVC框架,每一個請求最先訪問的都是DispatcherServlet,DispatcherServlet

spring mvc 配置詳解

app stl set方法 參數類型 utf-8 admin pathvaria 方案 -m 現在主流的Web MVC框架除了Struts這個主力 外,其次就是Spring MVC了,因此這也是作為一名程序員需要掌握的主流框架,框架選擇多了,應對多變的需求和業務時,可實行的

Spring MVC 配置.do 所在位置方法

tex text div log img 後綴 習慣 url tro 一。idea 生成的Spring MVC 項目將<url-pattern>.form<url-pattern>改成<url-pattern>.do<url-pat

spring mvc配置 + dbcp數據源+jdbcTemplate

roo cte con 官網 tor spirng nta 錯誤頁 工作 摘要: 把spring的jar包放到lib目錄,jar可以根據你要用的功能來選擇,如果懶或者不想以後用到功能再找就一起都放進去,註意不用放文檔jar和源碼jar 突然想起spring對環境的一些要求,

Spring Boot配置redis集群

rim ember bool private mon err rem exc gin 1、編寫redis.properties配置文件 spring.redis.cluster.nodes=172.16.19.128:6300,172.16.1.281:6302,172.

Spring MVC 配置及應用 註解配置(2)

SpringMVC 註解應用 /login.do 具體的步驟 -->DispatcherServlet -->HandlerMapping -->LoginController  -->ViewReslover -->login.jsp (1)&

Spring MVC 配置及應用(1)

一,分析 目的:通過控制器呼叫hello.jsp  /hello.do -->DispatcherServlet(配置) -->HanlderMapping(配置) -->HelloController(編寫+配置) -->ViewResolver -

Spring MVC配置檔案配置檢視解析器

spring mvc配置檔案 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="ht

Spring MVC 配置檔案dispatcher-servlet.xml 檔案詳解

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframew

Spring MVC配置不攔截HTML等資源請求

問題 <servlet-mapping> <servlet-name>dispatcher</servlet-name> <!--會攔截所有的請求--> <url-pattern>/</url

配置redis快取失效時間

最近想利用redis快取做一個簡陋版的類似騰訊新聞這樣的檢視新聞的網頁。用了快取以後,新增新聞快取沒有更新,想使用快取的失效時間做到資料庫快取一致性。剛開始做的時候認為使用@CachePut註解會起到更新快取的作用,設定了cacheName和key都和查詢方法中的@Cacheable中的

spring boot配置 Redis

pom依賴: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis<

Spring MVC配置詳解

來源:https://www.cnblogs.com/superjt/p/3309255.html 現在主流的Web MVC框架除了Struts這個主力 外,其次就是Spring MVC了,因此這也是作為一名程式設計師需要掌握的主流框架,框架選擇多了,應對多變的需求

Spring MVC配置JSP及配置thymeleaf

作者:譚東 環境為:IntelliJ Idea 2018.3版本 目前都是使用更加方便的Spring boot進行開發後端了,因為不用像Spring MVC這樣配置很多的配置檔案了。但是學習Spring MVC的常用配置,有助於我們更好的理解Spring boot為我們做了哪

Spring boot基於Redis快取商城分類,商品資訊

初始化分類以及商品資訊 @Component public class InitGoodsRedisData implements Applicat