国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - JAVA中 redisTemplate 和 jedis的配合使用操作

JAVA中 redisTemplate 和 jedis的配合使用操作

2021-08-08 14:39周永發(fā) Java教程

這篇文章主要介紹了JAVA中 redisTemplate 和 jedis的配合使用操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

首先項(xiàng)目A,也就是SpringBOOT項(xiàng)目中使用redisTemplate 來做REDIS的緩存時(shí),你會發(fā)現(xiàn)存到REDIS里邊的KEY和VALUE,redisTemplat使用jdkSerializeable存儲二進(jìn)制字節(jié)編碼

項(xiàng)目B中使用jedis時(shí),存儲起來的是字符串,導(dǎo)致項(xiàng)目A要調(diào)用項(xiàng)目緩存的鍵值對時(shí),獲取不到

解決方案:

修改項(xiàng)目A的redisTemplate的序列方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
  
  /**
   * redis模板,存儲關(guān)鍵字是字符串,值是Jdk序列化
   * @param factory
   * @return
   * @Description:
   */
  @Bean
  public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(factory);
    //key序列化方式;但是如果方法上有Long等非String類型的話,會報(bào)類型轉(zhuǎn)換錯(cuò)誤;
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long類型不可以會出現(xiàn)異常信息;
    redisTemplate.setKeySerializer(redisSerializer);
    redisTemplate.setHashKeySerializer(redisSerializer);
    //默認(rèn)使用JdkSerializationRedisSerializer序列化方式;會出現(xiàn)亂碼,改成StringRedisSerializer
    StringRedisSerializer stringSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(stringSerializer);
    return redisTemplate;
  }
}

補(bǔ)充:RedisTemplate初始化和創(chuàng)建(非Spring注入方式)

概述

在工作中, 可能會在非Spring項(xiàng)目(如Spark,Flink作業(yè))中去操作Redis, 重復(fù)造輪子去寫工具類沒有太大的意義, 使用RedisTemplate已經(jīng)足夠豐富和完善了,使用New的方式進(jìn)行創(chuàng)建即可, 不同的spring-data-redis的版本會略有不同, 下面以2.3.0和1.8.9做為示例.

2.3.0

maven

?
1
2
3
4
5
6
7
8
9
10
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-redis</artifactId>
  <version>2.3.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>3.3.0</version>
</dependency>

代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class RedisTest {
  public static void main(String[] args) {
    //單機(jī)模式
    RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
    rsc.setPort(6379);
    rsc.setPassword("123456");
    rsc.setHostName("192.168.1.1");
    //集群模式
    RedisClusterConfiguration rcc = new RedisClusterConfiguration();
    rcc.setPassword("123456");
    List<RedisNode> nodes = Collections.singletonList(new RedisNode("192.168.1.1", 6379));
    rcc.setClusterNodes(nodes);
    RedisTemplate<String, String> template = new RedisTemplate<>();
    //單機(jī)模式
    JedisConnectionFactory fac = new JedisConnectionFactory(rsc);
    //集群模式
    //JedisConnectionFactory fac = new JedisConnectionFactory(rcc);
    fac.afterPropertiesSet();
    template.setConnectionFactory(fac);
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    ValueOperations<String, String> op = template.opsForValue();
    final String key = "123_tmp";
    final String value = "abc";
    template.delete(key);
    op.set(key, value);
    assert Objects.equals(op.get(key), value);
  }
}

集群方式運(yùn)行報(bào)錯(cuò)

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled

解決

在redis.conf下將cluster-enabled改為yes

如果只有一個(gè)節(jié)點(diǎn), 改為單機(jī)模式

1.8.9

maven

?
1
2
3
4
5
6
7
8
9
10
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-redis</artifactId>
  <version>1.8.9.RELEASE</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import java.util.Objects;
public class RedisTest {
  public static void main(String[] args) {
    RedisTemplate<String, String> template = new RedisTemplate<>();
    JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());
    JedisShardInfo shardInfo = new JedisShardInfo("192.168.1.1", 6379);
    shardInfo.setPassword("123456");
    fac.setShardInfo(shardInfo);
    template.setConnectionFactory(fac);
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    ValueOperations<String, String> op = template.opsForValue();
    final String key = "123_tmp";
    final String value = "abc";
    template.delete(key);
    op.set(key, value);
    assert Objects.equals(op.get(key), value);
  }
}

這里有個(gè)小細(xì)節(jié), 如果不調(diào)用setShardInfo()方法, 那么需要執(zhí)行下面的代碼, afterPropertiesSet()用來初始化

?
1
2
3
4
5
JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());
    fac.setPort(6379);
    fac.setPassword("123456");
    fac.setHostName("192.168.1.1");
    fac.afterPropertiesSet();

說明

RedisTemplate的構(gòu)造方法有多種, 上面所舉例子為其中的一種; 不通過SpringBoot自動裝配的方式, 必須手動去執(zhí)行afterPropertiesSet()進(jìn)行初始化; 可參考SpringBoot整合redis的方式, 查看對應(yīng)實(shí)現(xiàn)

JAVA中 redisTemplate 和 jedis的配合使用操作

JAVA中 redisTemplate 和 jedis的配合使用操作

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

原文鏈接:https://www.cnblogs.com/yvanBk/p/10497542.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品成av人在线视午夜片 | 欧美日韩一区二区在线观看 | 久草中文在线观看 | 99国产精品久久久久久久成人热 | 懂色av中文字幕一区二区三区 | 久热免费在线观看 | 午夜在线电影 | 日韩中文字幕在线免费观看 | 国产精品成人av | 中文字幕日韩欧美一区二区三区 | 精品欧美乱码久久久久久1区2区 | 亚洲精品一区 | 国产精品日韩一区二区 | 精品国产一区二区三区久久久 | 久久精品夜夜夜夜夜久久 | 国产黄色播放 | 久久中文免费 | 久久精品亚洲国产 | 高清一区二区三区 | 操操网站 | 成人羞羞视频免费 | 国产乱码精品一区二区三区av | 成人在线播放 | 久久综合一区 | 久久男人天堂 | 九色在线观看 | 欧美日韩日本国产 | 欧美日韩国产精品一区二区 | 刘亦菲的毛片 | 亚洲国产精品久久人人爱 | 久久伦理电影网 | 久久久高清 | 羞羞视频在线看 | 黄色毛片网站在线观看 | 久久aⅴ国产欧美74aaa | 免费观看av大片 | 中文久久| 日韩中文字幕在线播放 | 亚洲va欧美va人人爽成人影院 | 欧美一级在线 | 欧美日韩精品在线播放 |