jedispool释放连接_redis项目实例

jedispool释放连接_redis项目实例1.下载redis,地址:https://download.csdn.net/download/rexueqingchun/103247192.设置redis密码,修改redis.windows.conf配置文件,去掉requirepassfoobared前面的注释  (注:foobared为默认密码,可以自定义密码,例如:123456)3.在redis目录中新建startup.bat,添加re…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1.下载redis,地址:https://download.csdn.net/download/rexueqingchun/10324719

2.设置redis密码,修改redis.windows.conf配置文件,去掉requirepass foobared前面的注释

  (注:foobared为默认密码,可以自定义密码,例如:123456)

3.在redis目录中新建startup.bat,添加redis-server.exe redis.windows.conf命令,通过startup.bat启动服务

4.项目中添加 jedis-2.1.0.jar,地址:https://download.csdn.net/download/rexueqingchun/10324985

5.项目中新建redis.properties文件:

#访问地址
redis.host=127.0.0.1
#访问端口
redis.port=6379
#注意,如果没有password,此处不设置值,但这一项要保留
redis.password=123456
#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
redis.maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制  
redis.maxActive=600
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWait=1000
#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;  
redis.testOnBorrow=true
#客户端连接超时时间
redis.timeout=30000
#可用数据库数
redis.database = 0

6.项目中新建redis-context.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans" 
		   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		   xmlns:p="http://www.springframework.org/schema/p" 
		   xmlns:tx="http://www.springframework.org/schema/tx"
		   xmlns:context="http://www.springframework.org/schema/context"
		   xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			   ">
	
	<!-- 加载redis属性文件  --> 
	<context:property-placeholder location="classpath*:/redis.properties" />
	
	<!-- redis连接池  --> 
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
		<property name="maxIdle" value="${redis.maxIdle}" />  
		<property name="maxActive" value="${redis.maxActive}" />  
		<property name="maxWait" value="${redis.maxWait}" />  
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />  
	</bean>  
	
	<!-- 单片机方式  -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
            <constructor-arg name="poolConfig" ref="poolConfig" />
            <constructor-arg name="host" value="${redis.host}" />
            <constructor-arg name="port" value="${redis.port}" type="int" />
            <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
            <constructor-arg name="password" value="${redis.password}" />
            <constructor-arg name="database" value="${redis.database}" type="int" />
        </bean>
	
        <!-- 集群方式  --> 
	<!-- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
    	<constructor-arg index="0" ref="poolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.host}" />
                </bean>
            </list>
        </constructor-arg>
        </bean> -->
</beans>			

 在web.xml中引入redis-context.xml文件:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/WEB-INF/applicationContext.xml;
		/WEB-INF/redis-context.xml;
	</param-value>
</context-param>

7.在service中注入jedisPool:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.highset.goexpo.util.JsonDateValueProcessor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {
	
	@Autowired
        private JedisPool jedisPool;//注入jedisPool
	
	//保存单个值
	public void set(String key,String value){
            Jedis jedis = jedisPool.getResource();
            jedis.set(key, value);
	}
	
	//获取单个值
	public String get(String key){
	    String value = "";
	    Jedis jedis = jedisPool.getResource();
            value = jedis.get(key);
            return value;
	}
	
	//根据key删除值
	public void del(String key){
	    Jedis jedis = jedisPool.getResource();
            jedis.del(key);
	}

	//保存list集合
	public void setList(String key,List<Map> list){
	    if(list != null){
	        Jedis jedis = jedisPool.getResource();
		for(Map m : list){
		    JsonConfig jsonConfig = new JsonConfig();  
		    //处理时间格式,注意时间类型java.sql.Timestamp/java.util.Date/java.sql.Date
		    jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));  
		    JSONObject jsonObject = JSONObject.fromObject(m,jsonConfig);  
                    String jsonString = jsonObject.toString(); 
                    jedis.rpush(key, jsonString);
	        }
	    }
	}
	
	//获取list集合
	public List<Map> getList(String key){
	    List<Map> listMap = new ArrayList<Map>();
	    Jedis jedis = jedisPool.getResource();
	    List<String> list = jedis.lrange(key, 0, -1);  
            for(String m : list){  
        	JSONObject jsonObject = JSONObject.fromObject(m);
                Map<Object, Object> map = (Map)jsonObject;
                listMap.add(map);
            }  
            return listMap;
	}
	
	//获取list分页数据
	public List<Map> getPageList(String key,int start,int end){
	    List<Map> listMap = new ArrayList<Map>();
	    Jedis jedis = jedisPool.getResource();
	    List<String> list = jedis.lrange(key, start, end);  
            for(String m : list){  
        	JSONObject jsonObject = JSONObject.fromObject(m);
                Map<Object, Object> map = (Map)jsonObject;
                listMap.add(map);
            }  
            return listMap;
	}
}

8.处理json日期格式工具类:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonDateValueProcessor implements JsonValueProcessor { 
	
    private String format ="yyyy-MM-dd hh:mm:ss";  
      
    public JsonDateValueProcessor() {  
        super();  
    }  
      
    public JsonDateValueProcessor(String format) {  
        super();  
        this.format = format;  
    }  
  
    @Override  
    public Object processArrayValue(Object paramObject,JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
  
    @Override  
    public Object processObjectValue(String paramString, Object paramObject,JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
      
    private Object process(Object value){  
        if(value instanceof Date){    
            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);    
            return sdf.format(value);  
        }    
        return value == null ? "" : value.toString();    
    }  
}  

附常用redis命令:

启动redis服务:redis-server.exe redis.windows.conf 
远程连接redis服务:redis-cli -h 127.0.0.1 -p 6379
设置key:set key value
获取key值:get key
查看是否设置密码:config get requirepass
登录密码:auth password

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197055.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 剖析PetShop 4

    剖析PetShop 4PetShop的系统架构设计前言:PetShop是一个范例,微软用它来展示.Net企业系统开发的能力。业界有许多.Net与J2EE之争,许多数据是从微软的PetShop和Sun的PetStore而来。这种争论不可避免带有浓厚的商业色彩,对于我们开发人员而言,没有必要过多关注。然而PetShop随着版本的不断更新,至现在基于.Net2.0的PetShop4.0为止,整个设计逐渐变得成熟而优雅,

    2022年10月17日
    2
  • 移植 libuv 至 Visual C++ 6.0 并支持 Windows XP 编译系统

    移植 libuv 至 Visual C++ 6.0 并支持 Windows XP 编译系统

    2022年1月16日
    86
  • Django(12)项目报错AttributeError: ‘bytes’ object has no attribute ‘encode’「建议收藏」

    Django(12)项目报错AttributeError: ‘bytes’ object has no attribute ‘encode’「建议收藏」报错情况Django使用makemigrations做数据迁移的时候报如下错误File"/Users/jkc/.virtualenvs/django_env/lib/python3.7

    2022年7月29日
    7
  • java flowable工作流_flowable工作流免费视频

    java flowable工作流_flowable工作流免费视频1.2基本操作1.2.1编码准备这里一官网请假流程案例为例resources目录下新建一个holiday-request.bpmn20.xml文件1.2.2初始化DB运行脚本后会初始化相关表,后面一点点介绍查看act_re_deployment表查看act_ge_bytearray,记录xml文本字节数组,通过deployment_id关联,查看相关的api有列表查询、分页查询等1.2.4发起流程修改holiday-request.bpmn2

    2025年7月11日
    3
  • tortoisegit使用教程_git小乌龟拉取代码

    tortoisegit使用教程_git小乌龟拉取代码一、下载之前需要下载三个安装包,分别是git、小乌龟客户端、小乌龟中文语言包:二、下载与配置:1.下载Git并且暗转,下载地址:https://git-for-windows.github.io/2.下载TortoiseGit客户端以及中文语言包地址:https://tortoisegit.org/download/此处省略一万个next3.配置TortoiseGit小乌龟首先选择自己需要进行管理的文件夹作为本地Git的仓库,我设置的是D:\A_Projects\OMS1.0然后在文

    2022年9月16日
    5
  • sockets: SCTP「建议收藏」

    sockets: SCTP「建议收藏」SCTP的流是关联内部具有先后顺序的消息队列。SCTP的多流特性可以减少头端阻塞,同一个流中的数据会延缓,不影响其他流。

    2022年6月26日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号