redis java 性能_一 java 中使用redis 测试Redis的写入性能

配置文件:

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:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd">

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

class="org.springframework.data.redis.serializer.StringRedisSerializer" />

实体类:

Role

package com.ssm.chapter17.pojo;

import java.io.Serializable;

public class Role implements Serializable {

private static final long serialVersionUID = 6977402643848374753L;

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;

}

}

工具类:

testJedis

package com.ssm.chapter17.jedis;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class JedisTest {

public void testJedis() {

Jedis jedis = testPool().getResource();

// Jedis jedis = new Jedis("localhost", 6379); //连接Redis

// jedis.auth("password");//如果需密码

int i = 0;// 记录操作次数

try {

long start = System.currentTimeMillis();// 开始毫秒数

while (true) {

long end = System.currentTimeMillis();

if (end - start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作

break;

}

i++;

jedis.set("test" + i, i + "");

}

} finally {// 关闭连接

jedis.close();

}

System.out.println("redis每秒操作:" + i + "次");// 打印1秒内对Redis的操作次数

}

private JedisPool testPool() {

JedisPoolConfig poolCfg = new JedisPoolConfig();

// 最大空闲数

poolCfg.setMaxIdle(50);

// 最大连接数

poolCfg.setMaxTotal(100);

// 最大等待毫秒数

poolCfg.setMaxWaitMillis(20000);

// 使用配置创建连接池

JedisPool pool = new JedisPool(poolCfg, "localhost");

// 从连接池中获取单个连接

Jedis jedis = pool.getResource();

// 如果需密码

// jedis.auth("password");

return pool;

}

}

测试类,有原生的方法,也有Spring 提供的方法:

package com.ssm.chapter17.main;

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 com.ssm.chapter17.jedis.JedisTest;

import com.ssm.chapter17.pojo.Role;

public class Chapter17Main {

public static void main(String[] args) {

// testSessionCallback();

testSpring();

}

private static void testJedis() {

JedisTest jedisTest = new JedisTest();

jedisTest.testJedis();

}

private static void testSpring() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

Role role = new Role();

role.setId(1L);

role.setRoleName("role_name_1");

role.setNote("note_1");

redisTemplate.opsForValue().set("role_1", role);

Role role1 = (Role) redisTemplate.opsForValue().get("role_1");

System.out.println(role1.getRoleName());

}

private static void testSessionCallback() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

Role role = new Role();

role.setId(1);

role.setRoleName("role_name_1");

role.setNote("role_note_1");

SessionCallback callBack = new SessionCallback() {

@Override

public Role execute(RedisOperations ops) throws DataAccessException {

ops.boundValueOps("role_1").set(role);

return (Role) ops.boundValueOps("role_1").get();

}

};

Role savedRole = (Role) redisTemplate.execute(callBack);

System.out.println(savedRole.getId());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值