redis 支撑许多言语的客户端。在官方网站上有支撑的一切的 [redis 客户端列表]。

  因为往常运用 java 作为开发言语,所以这儿描绘一下怎样通过 java 来联接和操作 redis 服务器。在官方文档中, Java 推荐的 redis 客户端是 Jedis ,这儿我们也用这个客户端对 redis 服务器进行操作。

  引进依托
  首要我们建立一个 maven 工程,在工程的 pom.xml 文件中参加 Jedis 的依托引证。为了便当查验,还参加了 Junit 依托。文件内容如下。

  
  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0

  com.x9710.common
  redis-util
  1.0-SNAPSHOT
  
  
  commons-logging
  commons-logging
  1.1.1
  
  
  log4j
  log4j
  1.2.17
  
  
  redis.clients
  jedis
  2.9.0
  

  
  junit
  junit
  4.12
  test
  
  
  

  创建联接类
  建立 redis 联接类 com.x9710.common.redis.RedisConnection 。(ecteema)内容如下
  package com.x9710.common.redis;

  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisPool;
  import redis.clients.jedis.JedisPoolConfig;

  public class RedisConnection {
  /**
  * redis 联接池配备信息
  */
  private JedisPoolConfig jedisPoolConfig;
  /**
  * redis 服务器地址
  */
  private String ip;

  /**
  * redis 服务器端口
  */
  private Integer port;

  /**
  * redis 服务器暗码
  */
  private String pwd;

  /**
  * redis 服务器联接超时时间
  */
  private Integer timeOut;

  /**
  * redis 联接客户端称谓
  */
  private String clientName = null;

  private JedisPool jedisPool;

  public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
  this.jedisPoolConfig = jedisPoolConfig;
  }

  public void setIp(String ip) {
  this.ip = ip;
  }

  public void setPort(Integer port) {
  this.port = port;
  }

  public void setPwd(String pwd) {
  this.pwd = pwd;
  }

  public void setTimeOut(Integer timeOut) {
  this.timeOut = timeOut;
  }

  public void setClientName(String clientName) {
  this.clientName = clientName;
  }

  private void buildConnection() {
  if (jedisPool == null) {
  if (jedisPoolConfig == null) {
  jedisPool = new JedisPool(new JedisPoolConfig(), ip, port, timeOut, pwd, 0, clientName);
  } else {
  jedisPool = new JedisPool(jedisPoolConfig, ip, port, timeOut, pwd, 0, clientName);
  }
  }
  }

  public Jedis getJedis() {
  buildConnection();
  if (jedisPool != null) {
  return jedisPool.getResource();
  }
  return null;
  }

  }

  编写查验
  用一个查验类 com.x9710.common.redis.test.RedisConnectionTest 来查验 rdis 联接功用.(gzhongLan

  package com.x9710.common.redis.test;

  import com.x9710.common.redis.RedisConnection;
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;
  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisPoolConfig;

  public class RedisConnectionTest {
  private RedisConnection redisConnection;

  @Before
  public void before() {
  JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  //设置 redis 联接池最大联接数量
  jedisPoolConfig.setMaxTotal(50);
  //设置 redis 联接池最大闲暇联接数量
  jedisPoolConfig.setMaxIdle(10);
  //设置 redis 联接池最小闲暇联接数量
  jedisPoolConfig.setMinIdle(1);
  redisConnection = new RedisConnection();
  redisConnection.setIp("10.110.2.56");
  redisConnection.setPort(52981);
  redisConnection.setPwd("hhSbcpotThgWdnxJNhrzwstSP20DvYOldkjf");
  redisConnection.setClientName(Thread.currentThread().getName());
  redisConnection.setTimeOut(600);
  redisConnection.setJedisPoolConfig(jedisPoolConfig);
  }

  @Test
  public void testPutGet() {
  Jedis jedis = redisConnection.getJedis();
  try {
  jedis.select(1);
  jedis.set("name","grace");
  Assert.assertTrue("grace".equals(jedis.get("name")));
  } finally {
  if (jedis != null) {
  jedis.close();
  }
  }
  }
  }

  在 ide 环境中实施查验用例,作用如下。

  image
  现在,我们就在 Java 中使用 Jedit 客户端建立和 redis 的联接并且能够实施操作。对应的代码发布到了 GitHub 中

Java中怎么合理的使用redis?的更多相关文章

  1. Java中Redis简单入门

    Redis是一个开源的,先进的 key-value 存储可用于构建高性能,可扩展的 Web 应用程序的解决方案. Redis官方网网站是:http://www.redis.io/,如下: Redis ...

  2. Java中Redis入门(1)

    Redis是一个开源的,先进的 key-value 存储可用于构建高性能,可扩展的 Web 应用程序的解决方案. Redis官方网网站是:http://www.redis.io/,如下: Redis ...

  3. 在java中使用redis

    在java中使用redis很简单,只需要添加jedist.jar,通过它的api就可以了.而且,api和redis的语法几乎完全相同.以下简单的测试: 参考:http://www.runoob.com ...

  4. JAVA中使用Redis

    上节讲解了如何在centos上安装redis,点击查看.本节我们学习在java中使用redis.需要将jedis-*.jar添加到classpath(点击下载),如果使用连接池还需要commons-p ...

  5. Redis学习记录之Java中的初步使用

    1.关于Redis redis下载地址:<span style="font-family: Arial, Helvetica, sans-serif;">http:// ...

  6. JAVA中通过Jedis操作Redis连接与插入简单库

    一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...

  7. Redis笔记(六):Java中使用Redis

    Java程序使用Redis 添加依赖包 Maven依赖方式 <dependency> <groupId>redis.clients</groupId> <ar ...

  8. Redis实战——Redis的pub/Sub(订阅与发布)在java中的实现

    借鉴:https://blog.csdn.net/canot/article/details/51938955 1.什么是pub/sub Pub/Sub功能(means Publish, Subscr ...

  9. java中通过配置文件的方式(Jedis驱动)使用Redis

    在java中使用Redis,实际上是将Redis的一些命令封装到Jedis的实体类中,然后进行调用.

随机推荐

  1. Hbase启动出问题 master.HMaster: Failed to become active master

    Hbase启动出问题 2019-12-15 09:59:57,183 WARN [hadoop:16000.activeMasterManager] hdfs.DFSClient: DFS Read ...

  2. CF620C Pearls in a Row

    CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...

  3. AFO!

    \(update:2019-12-13\) 成绩已经出了,我的OI生涯也算是正式结束了.虽然成绩并不满意,但好在也是收获了一个省一(虽然我不一定用).总的来说,作为正式选手不到两年半的OI之路走得并不 ...

  4. Codeforces Round #603 (Div. 2) C. Everyone is a Winner! 二分

    C. Everyone is a Winner! On the well-known testing system MathForces, a draw of n rating units is ar ...

  5. js判断浏览器是否安装或启用了flash的方法总结

    目录 # js判断浏览器是否安装或启用了flash的方法 # chrome浏览器启用flash插件的方法 # 参考 # js判断浏览器是否安装或启用了flash的方法 在传统浏览器,可以使用windo ...

  6. Java Arrays类方法

    1:概述 主要谈一谈 Java使用fork/koin类 实现的并发排序 以及对于Stream流的支持的splitetor mismatch()   ->  寻找两个数组 第一次出现数据不一致的下 ...

  7. sql慢查询工具(配置代码)

    # 在mysql的配置文件/etc/mysql/mysql.conf.d/mysqld.cnf[mysqld]中配置懒查询 slow_query_log = ON # 是否已经开启慢查询 long_q ...

  8. 五分钟搞定 HTTPS 配置,二哥手把手教

    01.关于 FreeSSL.cn FreeSSL.cn 是一个免费提供 HTTPS 证书申请.HTTPS 证书管理和 HTTPS 证书到期提醒服务的网站,旨在推进 HTTPS 证书的普及与应用,简化证 ...

  9. Android中几种常用的定时器和延时方法

    通过实际项目的练习,掌握了几种android基本定时器和延时的用法,这里我想总结一下作为自己的收获,下面列出的是比较简洁的模式,方便简单地在程序中直接调用. 一.三种常用的定时器 1.Handler类 ...

  10. 动软生成Model(dapper.common)

    <#@ template language="c#" HostSpecific="True" #><#@ output extension= ...