当前位置: 首页 > news >正文

手机参数对比的网站最有效的推广学校的方式

手机参数对比的网站,最有效的推广学校的方式,wordpress 前端编辑器,java和php做网站谁好实现原理 需要配置好两个数据源,创建两个RedisTemplate在配置类中注入两个RedisConnectionFactory,分别创建对应的RedisTemplate进行操作 详解 配置数据源 我这里是在之前已有一个配置下面另外加了一个 spring:redis:# 地址host: localh…

实现原理

  1. 需要配置好两个数据源,创建两个RedisTemplate
  2. 在配置类中注入两个RedisConnectionFactory,分别创建对应的RedisTemplate进行操作

详解

配置数据源

我这里是在之前已有一个配置下面另外加了一个

spring:redis:# 地址host: localhost# 端口,默认为6379port: 6379# 数据库索引database: 0# 密码password: xxxx# 连接超时时间timeout: 10slettuce:pool:# 连接池中的最小空闲连接min-idle: 0# 连接池中的最大空闲连接max-idle: 8# 连接池的最大数据库连接数max-active: 8# #连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1ms#第二数据源second:host: 127.0.0.1port: 6379password: xxx

创建配置文件


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;@Configuration
public class RedisConfigs {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Value("${spring.redis.password}")private String password;@Value("${spring.redis.second.host}")private String secondHost;@Value("${spring.redis.second.port}")private int secondPort;@Value("${spring.redis.second.password}")private String secondPassword;@Bean(name = "redisConnectionFactory")@Primary  //默认选择这个数据源进行执行@Qualifier("redisConnectionFactory")public RedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();config.setHostName(host);config.setPort(port);config.setPassword(RedisPassword.of(password));return new JedisConnectionFactory(config);}@Bean(name = "secondRedisConnectionFactory")@Qualifier("secondRedisConnectionFactory")public RedisConnectionFactory secondRedisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();config.setHostName(secondHost);config.setPort(port);config.setPassword(RedisPassword.of(secondPassword));return new JedisConnectionFactory(config);}}

创建工具类

注入配置类中的RedisConnectionFactory分别生成对应的RedisTemplate,然后进行操作就可以啦

import com.bdtd.limit.common.config.FastJson2JsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;@Component
public class RedisUtil {private final RedisConnectionFactory redisConnectionFactory;private final RedisConnectionFactory secondRedisConnectionFactory;@Autowiredpublic RedisUtil(RedisConnectionFactory redisConnectionFactory,@Qualifier("secondRedisConnectionFactory")RedisConnectionFactory secondRedisConnectionFactory) {this.redisConnectionFactory = redisConnectionFactory;this.secondRedisConnectionFactory = secondRedisConnectionFactory;}public RedisTemplate<String, Object> getRedisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);// 使用StringRedisSerializer来序列化和反序列化redis的key值redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(serializer);redisTemplate.afterPropertiesSet();return redisTemplate;}public RedisTemplate<String, Object> getSecondRedisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(secondRedisConnectionFactory);FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);// 使用StringRedisSerializer来序列化和反序列化redis的key值redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(serializer);redisTemplate.afterPropertiesSet();return redisTemplate;}/*** 第一数据源*  缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值*/public <T> void setCacheObject(final String key, final T value){getRedisTemplate().opsForValue().set(key, value);}/***  第一数据源* 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度*/public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit){getRedisTemplate().opsForValue().set(key, value, timeout, timeUnit);}/***  第一数据源* 设置有效时间** @param key Redis键* @param timeout 超时时间* @param unit 时间单位* @return true=设置成功;false=设置失败*/public boolean expire(final String key, final long timeout, final TimeUnit unit){return getRedisTemplate().expire(key, timeout, unit);}/***  第一数据源* 判断 key是否存在** @param key 键* @return true 存在 false不存在*/public Boolean hasKey(String key){return getRedisTemplate().hasKey(key);}/***  第一数据源* 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public Object getCacheObject(final String key){ValueOperations<String, Object> operation = getRedisTemplate().opsForValue();return operation.get(key);}/***  第一数据源* 删除单个对象** @param key*/public boolean deleteObject(final String key){return getRedisTemplate().delete(key);}/***  第一数据源* 删除集合对象** @param collection 多个对象* @return*/public boolean deleteObject(final Collection collection){return getRedisTemplate().delete(collection) > 0;}/***  第一数据源* 缓存List数据** @param key 缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public <T> long setCacheList(final String key, final List<T> dataList){Long count = getRedisTemplate().opsForList().rightPushAll(key, dataList);return count == null ? 0 : count;}/***  第一数据源* 获得缓存的list对象** @param key 缓存的键值* @return 缓存键值对应的数据*/public <T> List<T> getCacheList(final String key){return (List<T>) getRedisTemplate().opsForList().range(key, 0, -1);}/***  第一数据源* 缓存Map** @param key* @param dataMap*/public <T> void setCacheMap(final String key, final Map<String, T> dataMap){if (dataMap != null) {getRedisTemplate().opsForHash().putAll(key, dataMap);}}/***  第一数据源* 获得缓存的Map** @param key* @return*/public <T> Map<Object, Object> getCacheMap(final String key){return getRedisTemplate().opsForHash().entries(key);}/***  第二数据源* 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值*/public <T> void setSencondCacheObject(final String key, final T value){getSecondRedisTemplate().opsForValue().set(key, value);}/***  第二数据源* 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度*/public <T> void setSencondCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit){getSecondRedisTemplate().opsForValue().set(key, value, timeout, timeUnit);}/***  第二数据源* 设置有效时间** @param key Redis键* @param timeout 超时时间* @param unit 时间单位* @return true=设置成功;false=设置失败*/public boolean sencondExpire(final String key, final long timeout, final TimeUnit unit){return getSecondRedisTemplate().expire(key, timeout, unit);}/***  第二数据源* 判断 key是否存在** @param key 键* @return true 存在 false不存在*/public Boolean sencondHasKey(String key){return getSecondRedisTemplate().hasKey(key);}/***  第二数据源* 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public Object getSencondCacheObject(final String key){ValueOperations<String, Object> operation = getSecondRedisTemplate().opsForValue();return operation.get(key);}/***  第一数据源* 删除单个对象** @param key*/public boolean deleteSencondObject(final String key){return getSecondRedisTemplate().delete(key);}/***  第二数据源* 删除集合对象** @param collection 多个对象* @return*/public boolean deleteSencondObject(final Collection collection){return getSecondRedisTemplate().delete(collection) > 0;}/***  第二数据源* 缓存List数据** @param key 缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public <T> long setSencondCacheList(final String key, final List<T> dataList){Long count = getSecondRedisTemplate().opsForList().rightPushAll(key, dataList);return count == null ? 0 : count;}/***  第二数据源* 获得缓存的list对象** @param key 缓存的键值* @return 缓存键值对应的数据*/public <T> List<T> getSencondCacheList(final String key){return (List<T>) getSecondRedisTemplate().opsForList().range(key, 0, -1);}/***  第二数据源* 缓存Map** @param key* @param dataMap*/public <T> void setSencondCacheMap(final String key, final Map<String, T> dataMap){if (dataMap != null) {getSecondRedisTemplate().opsForHash().putAll(key, dataMap);}}/***  第二数据源* 获得缓存的Map** @param key* @return*/public <T> Map<Object, Object> getSencondCacheMap(final String key){return getSecondRedisTemplate().opsForHash().entries(key);}}

Redis使用FastJson序列化


import java.nio.charset.Charset;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;/*** Redis使用FastJson序列化* */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private Class<T> clazz;public FastJson2JsonRedisSerializer(Class<T> clazz){super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) throws SerializationException{if (t == null){return new byte[0];}return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);}@Overridepublic T deserialize(byte[] bytes) throws SerializationException{if (bytes == null || bytes.length <= 0){return null;}String str = new String(bytes, DEFAULT_CHARSET);return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);}
}
http://www.khdw.cn/news/8830.html

相关文章:

  • 做编辑器的网站长沙县网络营销咨询
  • 谷歌做自己的网站最新清远发布
  • 在线logo生成器标智客搜索引擎优化的七个步骤
  • 如何做网站webstormweb网页制作成品
  • 专业建设网站哪个好上海牛巨微网络科技有限公司
  • win7做网站服务器无锡网站制作
  • 浙江建设信息港怎么查询优化营商环境个人心得
  • 可以做数学题的网站网站运营推广方式
  • 网站开发技术负责那些seo教程视频论坛
  • jsp python 网站开发微信引流的十个方法
  • 网站目录怎么做网上接单平台
  • 网站建设如何报价域名注册管理机构
  • 网站建设费用计入管理费用的哪个科目河北百度推广客服电话
  • 个人网站介绍怎么写关键词挖掘
  • 湖南省建设厅电话win7系统优化软件
  • 做公司网站需要备案吗小红书关键词热度查询
  • 沈阳微网站建设最新新闻实时新闻
  • 2018做网站的视频seo北京网站推广
  • 网站开发w亿玛酷1流量订制搜索引擎优化 简历
  • 不属于web2.0网站开发百度网盘搜索神器
  • foxmail邮箱手机版app网站seo是干什么的
  • 医疗网站制作2345王牌浏览器
  • 外贸网站布局武汉百度开户电话
  • 电子商务网站开发合同新开发的app怎么推广
  • 长安手机网站建设推广赚钱一个50元
  • 响应式做的比较好的网站永久免费客服系统
  • 批量替换wordpress页面文字汕头最好的seo外包
  • 上海专业做网站站内seo内容优化包括
  • 青岛seo整站优化哪家专业百度经验悬赏任务平台
  • 海外贸易网站百度营业执照怎么办理