Java后台代码操作Redis

  • Jedis

import org.junit.jupiter.api.Test;

import redis.clients.jedis.Jedis;

class TestJ {

	@Test
	void test() {
		Jedis jedis=new Jedis("127.0.0.1",6379);
		//jedis.auth("密码");
		int i=0;
		try {
			long start=System.currentTimeMillis();
			while(true) {
				long end=System.currentTimeMillis();
				if(end-start>=1000) {
					break;
				}
				i++;
				jedis.set("test"+i,i+"");
			}
		}finally {
			jedis.close();
		}
		System.out.println("redis每秒操作了"+i+"次");
	}

}

  • Spring中使用Redis

RedisTemple

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">	
        
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50"/>
<!-- 最大连接数 -->
<property name="maxTotal" value="100"/>
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000"/>
</bean>      

<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<!-- <property name="password" value="**********"/> -->
<property name="poolConfig" ref="poolConfig"/>
</bean>

<!-- 序列化 -->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>

</beans> 
package pojo;

import java.io.Serializable;
/**
 * 对象要可序列化,一定要实现Serializable接口
 *
 */
public class Role implements Serializable{
	private static final long serialVersionUID = 1L;
	private long id;
	 private String roleName;
	 private String note;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	 
}
package redisTemple;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import pojo.Role;

class TestRT {

	@Test
	void test() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		RedisTemplate<String, Role> rt=ac.getBean(RedisTemplate.class);
		Role role=new Role();
		role.setId(1L);
		role.setRoleName("lp");
		role.setNote("罗飘");
		rt.opsForValue().set("role1", role);
		Role role1=rt.opsForValue().get("role1");
		System.out.println(role1.getId()+role1.getRoleName()+role1.getNote());
	}

}


SessionCallback保证所有操作来自同一个连接

package sessionCallback;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import pojo.Role;

class TestSC {

	@Test
	void test() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		RedisTemplate<String, Role> rt=ac.getBean(RedisTemplate.class);
		Role role=new Role();
		role.setId(1L);
		role.setRoleName("lp");
		role.setNote("罗飘");
		/**
		 * 保证所有操作来自同一个连接
		 */
		SessionCallback<Role> sc=new SessionCallback<Role>() {
			@Override
			public Role execute(RedisOperations ops) throws DataAccessException {
				ops.boundValueOps("role2").set(role);
				return (Role) ops.boundValueOps("role2").get();
			}
			
		};
		
		Role saveRole=(Role)rt.execute(sc);
		System.out.println(saveRole.getId()+saveRole.getRoleName()+saveRole.getNote());
	}

}

Spring操作Redis不同数据结构

字符串操作:

package data.structure;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

public class TestDS {
	@Test
	public void testString(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		RedisTemplate<String,String> rt=ac.getBean(RedisTemplate.class);
		//设值
		rt.opsForValue().set("testString1", "你好");
		rt.opsForValue().set("testString2", "世界");
		//取值
		String value1=rt.opsForValue().get("testString1");
		String value2=rt.opsForValue().get("testString2");
		System.out.println(value1+value2);
		//删除
		rt.delete("testString2");
		//长度
		Long length=rt.opsForValue().size("testString1");
		System.out.println(length);
		//更新返旧值
		String oldV=rt.opsForValue().getAndSet("testString1", "哈哈123456");
		System.out.println(oldV);
		//查看更新后的值
		value1=rt.opsForValue().get("testString1");
		System.out.println(value1);
		//求子串
		String sub=rt.opsForValue().get("testString1",7,9);
		System.out.println(sub);
		//追加
		int newLen=rt.opsForValue().append("testString1", "_append");
		System.out.println(newLen);
		value1=rt.opsForValue().get("testString1");
		System.out.println(value1);	
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37575994/article/details/100551083
今日推荐