侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

java 里使用 1

Dettan
2022-01-18 / 0 评论 / 0 点赞 / 54 阅读 / 3,965 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
方法名都和操作的名一样

package com.yh.test;

import org.junit.Test;

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

public class JedisTest {

@Test
public void fun(){
	//创建jedis客户端对象
	Jedis jedis = new Jedis(&quot;172.16.31.22&quot;, 6379);
	//向缓存库中存放数据类型为字符串的数据
	jedis.set(&quot;name&quot;, &quot;马光哲&quot;);
	//从缓存库中获取数据
	String name = jedis.get(&quot;name&quot;);
	System.out.println(&quot;name ==&quot;+name);
	jedis.close();
}

@Test
public void fun2(){
	JedisPoolConfig  jpc = new JedisPoolConfig();
	jpc.setMaxIdle(20);
	jpc.setMinIdle(5);
	JedisPool jp = new JedisPool(jpc, &quot;172.16.31.22&quot;, 6379);
	Jedis jedis = jp.getResource();
	String name = jedis.get(&quot;name&quot;);
	System.out.println(&quot;name ==&quot;+name);
	jedis.expire(&quot;name&quot;, 20);
	
	System.out.println(jedis.ttl(&quot;name&quot;));
	jedis.close();
	jp.close();
}

}






spring 集成
接口
package com.fz.common.component;

public interface RedisUtil {
public String set(String key, String value);

public String get(String key);

public Long del(String key);

public void expire(String key, Integer second);

public Long ttl(String key);

public Long incr(String key);

public Long hset(String key, String hkey, String value);

public String hget(String key, String hkey);

public Long hdel(String key, String hkey);

}

单机版
public class RedisSingleUtil implements RedisUtil {
    private JedisPool jedisPool;
public JedisPool getJedisPool() {
    return jedisPool;
}

public void setJedisPool(JedisPool jedisPool) {
    this.jedisPool = jedisPool;
}

@Override
public String set(String key, String value) {
    Jedis resource = jedisPool.getResource();
    String set = resource.set(key, value);
    resource.close();
    return set;
}</code></pre></div>            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
            <div style="color: inherit; fill: inherit;">
                <div style="display: flex;">
spring 配置文件
<bean id="redisSingle" class="com.fz.common.component.impl.RedisSingleUtil">
    <property name="jedisPool" ref="jedisPool"/>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="host" value="localhost" />
    <constructor-arg name="port" value="6379" />
</bean>
之后用@AutoWaried就可以注入了
集群版
package com.fz.common.component.impl;

import com.fz.common.component.RedisUtil;
import redis.clients.jedis.JedisCluster;

public class RedisClusterUtil implements RedisUtil {

private JedisCluster jedisCluster;

@Override
public String set(String key, String value) {
    String set = jedisCluster.set(key, value);
    return set;
}

@Override
public String get(String key) {
    return jedisCluster.get(key);

}

@Override
public Long del(String key) {
    return jedisCluster.decr(key);
}

@Override
public void expire(String key, Integer second) {
    jedisCluster.expire(key, second);
}

@Override
public Long ttl(String key) {
    return jedisCluster.ttl(key);
}

@Override
public Long incr(String key) {
    return jedisCluster.incr(key);
}

}

<bean id="redisCluster" class="com.fz.common.component.impl.RedisClusterUtil">
    <property name="jedisCluster" ref="jedisCluster"></property>
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg>
        <set>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.198.153"/>
                <constructor-arg name="port" value="7001"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.198.153"/>
                <constructor-arg name="port" value="7002"/>
            </bean>
        </set>
    </constructor-arg>
</bean>
            <div style="color: inherit; fill: inherit;">
                <div style="display: flex;">

0

评论区