• RedisJava基础代码实现


    Jedis快速入门

    1. <!--jedis-->
    2. <dependency>
    3. <groupId>redis.clients</groupId>
    4. <artifactId>jedis</artifactId>
    5. <version>3.7.0</version>
    6. </dependency>
    7. <!--单元测试-->
    8. <dependency>
    9. <groupId>org.junit.jupiter</groupId>
    10. <artifactId>junit-jupiter</artifactId>
    11. <version>5.7.0</version>
    12. <scope>test</scope>
    13. </dependency>
    1. public class JedisConnectionFacotry {
    2. private static final JedisPool jedisPool;
    3. static {
    4. //配置连接池
    5. JedisPoolConfig poolConfig = new JedisPoolConfig();
    6. poolConfig.setMaxTotal(8);
    7. poolConfig.setMaxIdle(8);
    8. poolConfig.setMinIdle(0);
    9. poolConfig.setMaxWaitMillis(1000);
    10. //创建连接池对象
    11. jedisPool = new JedisPool(poolConfig,
    12. "192.168.150.101",6379,1000,"123321");
    13. }
    14. public static Jedis getJedis(){
    15. return jedisPool.getResource();
    16. }
    17. }
    1. @BeforeEach
    2. void setUp(){
    3. //建立连接
    4. /*jedis = new Jedis("127.0.0.1",6379);*/
    5. jedis = JedisConnectionFacotry.getJedis();
    6. //选择库
    7. jedis.select(0);
    8. }
    9. @AfterEach
    10. void tearDown() {
    11. if (jedis != null) {
    12. jedis.close();
    13. }
    14. }

    Redis的Java客户端-SpringDataRedis

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.7version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.heimagroupId>
    12. <artifactId>redis-demoartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>redis-demoname>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-data-redisartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.apache.commonsgroupId>
    26. <artifactId>commons-pool2artifactId>
    27. dependency>
    28. <dependency>
    29. <groupId>com.fasterxml.jackson.coregroupId>
    30. <artifactId>jackson-databindartifactId>
    31. dependency>
    32. <dependency>
    33. <groupId>org.projectlombokgroupId>
    34. <artifactId>lombokartifactId>
    35. <optional>trueoptional>
    36. dependency>
    37. <dependency>
    38. <groupId>org.springframework.bootgroupId>
    39. <artifactId>spring-boot-starter-testartifactId>
    40. <scope>testscope>
    41. dependency>
    42. dependencies>
    43. <build>
    44. <plugins>
    45. <plugin>
    46. <groupId>org.springframework.bootgroupId>
    47. <artifactId>spring-boot-maven-pluginartifactId>
    48. <configuration>
    49. <excludes>
    50. <exclude>
    51. <groupId>org.projectlombokgroupId>
    52. <artifactId>lombokartifactId>
    53. exclude>
    54. excludes>
    55. configuration>
    56. plugin>
    57. plugins>
    58. build>
    59. project>
    1. spring:
    2. redis:
    3. host: 192.168.150.101
    4. port: 6379
    5. password: 123321
    6. lettuce:
    7. pool:
    8. max-active: 8 #最大连接
    9. max-idle: 8 #最大空闲连接
    10. min-idle: 0 #最小空闲连接
    11. max-wait: 100ms #连接等待时间
    1. @SpringBootTest
    2. class RedisDemoApplicationTests {
    3. @Autowired
    4. private RedisTemplate redisTemplate;
    5. @Test
    6. void testString() {
    7. // 写入一条String数据
    8. redisTemplate.opsForValue().set("name", "虎哥");
    9. // 获取string数据
    10. Object name = redisTemplate.opsForValue().get("name");
    11. System.out.println("name = " + name);
    12. }
    13. }

    数据序列化器

    1. @Configuration
    2. public class RedisConfig {
    3. @Bean
    4. public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
    5. // 创建RedisTemplate对象
    6. RedisTemplate template = new RedisTemplate<>();
    7. // 设置连接工厂
    8. template.setConnectionFactory(connectionFactory);
    9. // 创建JSON序列化工具
    10. GenericJackson2JsonRedisSerializer jsonRedisSerializer =
    11. new GenericJackson2JsonRedisSerializer();
    12. // 设置Key的序列化
    13. template.setKeySerializer(RedisSerializer.string());
    14. template.setHashKeySerializer(RedisSerializer.string());
    15. // 设置Value的序列化
    16. template.setValueSerializer(jsonRedisSerializer);
    17. template.setHashValueSerializer(jsonRedisSerializer);
    18. // 返回
    19. return template;
    20. }
    21. }

            整体可读性有了很大提升,并且能将Java对象自动的序列化为JSON字符串,并且查询时能自动把JSON反序列化为Java对象。不过,其中记录了序列化时对应的class名称,目的是为了查询时实现自动反序列化。这会带来额外的内存开销。

    StringRedisTemplate

  • 相关阅读:
    MySQL(进阶)--索引
    Ehcache 缓存框架的使用
    工作常用之Spark调优一】
    docker安装与搭建sqli靶机
    【Python】pyinstaller打包百科全书
    30m退耕还湿空间数据集(2000-2010年,西南地区)
    Python基础入门篇【28】--python初识面向对象
    Android Studio 项目引入ProtoBuf(附序列化与反序列化)
    李宏毅《DLHLP》学习笔记7 - Voice Conversion 1
    这也能造成故障?我只给DTO类加了一个属性
  • 原文地址:https://blog.csdn.net/qq_45412648/article/details/132760608