redis 幾種資料類型往資料庫存資料和取資料的協助類

來源:互聯網
上載者:User

標籤:

package com.fndsoft.bcis.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import org.springframework.stereotype.Service;import java.util.*;/** * redis緩衝協助類 * Created by DELL on 2016/5/23. */@Servicepublic class RedisCacheUtil<T> {    @Autowired    public RedisTemplate redisTemplate;    /**     * 緩衝基本的對象,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.leftPush(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(listOperation.index(key,i));        }        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);        Iterator<T> it = dataSet.iterator();        while (it.hasNext()) {            setOperation.add(it.next());        }        return setOperation;    }    /**     * 獲得緩衝的set     *     * @param key     * @return     */    public Set<T> getCacheSet(String key) {        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()) {                hashOperations.put(key, entry.getKey(), entry.getValue());            }        }        return hashOperations;    }    /**     * 獲得緩衝的Map     *     * @param key     * @return     */    public <T> Map<String, T> getCacheMap(String key) {        Map<String, T> map = redisTemplate.opsForHash().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()) {                hashOperations.put(key, entry.getKey(), entry.getValue());            }        }        return hashOperations;    }    /**     * 獲得緩衝的Map     *     * @param key     * @return     */    public <T> Map<Integer, T> getCacheIntegerMap(String key) {        Map<Integer, T> map = redisTemplate.opsForHash().entries(key);        return map;    }}

以下是儲存list對象的測試方法:

 /**     * redis緩衝list對象     */    @Test    public void setCatchValueForList() {        String key = "user_Test7";        List<Code> codeList = new ArrayList<>();        Code code1 = new Code("436436", 533);        Code code2 = new Code("214214", 53453);        codeList.add(code1);        codeList.add(code2);        redisCacheUtil.setCacheList(key, codeList);        System.out.println("儲存資料成功!");    }    @Test    public void getValueForList() {        String key = "user_Test7";        List<Code> codeList = redisCacheUtil.getCacheList(key);        for (Code code : codeList) {            System.out.println(code.getName() + " " + code.getAge());        }    }

 

redis 幾種資料類型往資料庫存資料和取資料的協助類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.