栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring整合Redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring整合Redis

Spring整合Redis
  • 一,maven导入依赖
  • 二,Spring整合
    • (1)SpringBoot
    • (2)Spring
  • 三,使用RedisCacheUtil工具类

一,maven导入依赖
 
    redis.clients
    jedis
    2.9.0


    org.springframework.data
    spring-data-redis
    1.6.2.RELEASE

二,Spring整合 (1)SpringBoot
spring:
  # 资源信息
  messages:
    # 国际化资源文件路径
    basename: i18n/messages
  profiles: 
    active: druid
  # 文件上传
  servlet:
     multipart:
       # 单个文件大小
       max-file-size:  10MB
       # 设置总上传的文件大小
       max-request-size:  20MB
  # 服务模块
  devtools:
    restart:
      # 热部署开关
      enabled: true
  # redis 配置
  redis:
    # 地址
    host: localhost
    # 端口,默认为6379
    port: 6379
    # 数据库索引
    database: 0
    # 密码
    password: 
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
(2)Spring
	# 服务器ip
	redis.hostname = 127.0.0.1
	
	# redis数据库端口
	redis.port = 6379
	
	# 使用的数据库(共有16个数据库0~15)
	redis.database = 2
	
	# 控制一个pool可分配多少个jedis示例
	redis.pool.maxActive = 50
	
	# 控制一个pool最多有多少个状态为idle的jedis实例
	redis.pool.maxIdle = 300
	
	# 最大等待连接时间(单位毫秒)
	redis.pool.maxTotal = 600
	
	# redis密码一般不设置密码,设了重启服务也会没有
	redis.pass =
   
    

    
    
        
        
    

    
    
        
        
        
        
        
        
    

    
    
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    

三,使用RedisCacheUtil工具类
package com.hcepms.base.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;


@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCacheUtil {

    @Autowired
    public RedisTemplate redisTemplate;

    
    public void setCacheObject(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    
    public  void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    
    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    
    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    
    public  T getCacheObject(final String key) {
        ValueOperations operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

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

    
    public void deleteObject(final Collection collection) {
        redisTemplate.delete(collection);
    }

    
    public  long setCacheList(final String key, final List dataList) {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }

    
    public  List getCacheList(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    
    public  BoundSetOperations setCacheSet(final String key, final Set dataSet) {
        BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
        Iterator it = dataSet.iterator();
        while (it.hasNext()) {
            setOperation.add(it.next());
        }
        return setOperation;
    }

    
    public  Set getCacheSet(final String key) {
        return redisTemplate.opsForSet().members(key);
    }

    
    public  void setCacheMap(final String key, final Map dataMap) {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    
    public  Map getCacheMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    
    public  void setCacheMapValue(final String key, final String hKey, final T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    
    public  T getCacheMapValue(final String key, final String hKey) {
        HashOperations opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }

    
    public void delCacheMapValue(final String key, final String hkey) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.delete(key, hkey);
    }

    
    public  List getMultiCacheMapValue(final String key, final Collection hKeys) {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    
    public Collection keys(final String pattern) {
        return redisTemplate.keys(pattern);
    }
}



转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号