纯Java对Redis的基本操作

纯Java对Redis的基本操作

redis的安装在上一篇文字已经写过,redis初学

1.首先看一下整个项目的结构
这里写图片描述
2.由于我的这个项目是maven项目,所以需要在pom.xml文件中添加依赖
这是项目pom文件的全部代码,其中有软件自动生成的一些

<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>javaRedis</groupId>
  <artifactId>javaRedis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name/>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency> 
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.annotation</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.ejb</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.weld</groupId>
      <artifactId>weld-osgi-bundle</artifactId>
      <version>1.0.1-SP3</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.servlet</artifactId>
      <version>3.0.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

3.RedisUtil类,

package redis;

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

public class RedisUtil {
    //服务器ip
    private String address="192.168.80.210";

    //redis端口号
    private int port=6379;

    //访问密码
    private String password="redis";

    //连接redis等待时间
    private int timeOut=5000;

    //可用链接实例的最大数目,默认值为8
    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个
    //jedis实例,则此时pool的状态为exhausted(耗尽)
    private int maxTotal=1024;

    //控制一个pool最多有多个状态为idle(空闲的)的jedis实例
    //默认值也是8
    private int maxIdel=200;

    //等待可用链接的最大时间,单位毫秒,默认值为-1,表示永久不超时。
    //如果超过等待时间,则直接抛出JedisConnectionException
    private int maxWait=10000;

    //在borrow一个jedis实例时,是否提前进行validate操作
    //如果为True,则得到的jedis 实例均是可用的
    private boolean testOnBorrow=true;

    //连接池
    private JedisPool jedisPool=null;

    //构造函数
    public RedisUtil() {
        try {
            JedisPoolConfig config=new JedisPoolConfig();
            config.setMaxIdle(maxTotal);
            config.setMaxIdle(maxIdel);
            config.setMaxWaitMillis(maxWait);
            config.setTestOnBorrow(testOnBorrow);
            jedisPool=new JedisPool(config, address,port,timeOut,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取jedis实例
    public Jedis getJedis(){
        if(jedisPool!=null){
            return jedisPool.getResource();
        }
        return null;
    }


}

4.测试方法

package redis;


import java.util.Iterator;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisTest {
    private RedisUtil redisUtil=new RedisUtil();

    //字符串操作

    public void testStr(){
        Jedis jedis=redisUtil.getJedis();
        jedis.set("id", "187517");
        String id=jedis.get("id");
        System.out.println(id);
        jedis.close();
    }

    //存储List
    public void testList(){
        Jedis jedis=redisUtil.getJedis();
        //存储到列表中
        jedis.lpush("list", "redis");
        jedis.lpush("list", "java");
        jedis.lpush("list", "mysql");

        //读取存储的数据并输出
        List<String> list=jedis.lrange("list",0, 2);
        System.out.println("list中的数据为:");
        for (int i = 0; i < list.size(); i++) {

            System.out.println(list.get(i));
        }
    }

    //存储set
    public void testSet(){
        Jedis redis=redisUtil.getJedis();

        //存储数据
        redis.sadd("setTest1", "abc");
        redis.sadd("setTest1", "abcd");
        redis.sadd("setTest1", "abcde");

        //获取数据
        Set<String> keys=redis.smembers("setTest1");
        Iterator<String> it=keys.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }



    public static void main(String[] args) {
        RedisTest t=new RedisTest();
        t.testSet();
    }
}

记录
后面会细说和spring整合的redis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值