关于Redis安装使用

作为一名初级Java工程师,现将Redis的安装使用过程记录下来,摘自书本。

这是书上推荐地址:https://github.com/ServiceStack/redis-windows/tree/master/downloads

下一个版本解压即可。

新建startup.cmd,编辑输入redis-server redis.windows.conf

双击打开这个新建文件,启动成功-这是Windows版本,开发学习中使用

$ cd /usr/

$ mkdir redis    --创建redis目录

$ cd redis

$ wget http://download.redis.io/releases/redis-3.2.4.tar.gz    --获取redis版本

$ tar xzf redis-3.2.4.tar.gz    --解压缩

$ cd redis-3.2.4    

$ make    --运行

最后在当前目录执行src/redis-server    --打开服务

在Java中使用Redis,需要有两个jar包:jedis.jar和commons-pool2-2.4.2.jar

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

public class ConnectionTest {

    public void testJedis(){
        //链接Redis
        Jedis jedis = testPool().getResource();
        //Jedis jedis =new Jedis("localhost",6379);
        //jedis.auth("password");//如果需要密码
        //记录操作次数
        int i =0;
        try {
            //开始毫秒数
            long start= System.currentTimeMillis();
            while(true){
                long end=System.currentTimeMillis();
                if (end-start>=1000) {//当大于等于1000毫秒时,结束操作
                    break;
                }
                i++;
                jedis.set("test"+i, i+"");
            }
        }finally {//关闭链接
            jedis.close();
        }
        System.out.println("redis每秒操作:"+i+"次");//打印1秒内对Redis的操作次数
    }
    private JedisPool testPool(){
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        //最大空闲数
        poolConfig.setMaxIdle(50);
        //最大连接数
        poolConfig.setMaxTotal(100);
        //最大等待毫秒数
        poolConfig.setMaxWaitMillis(20000);
        //使用配置创建连接池
        JedisPool pool=new JedisPool(poolConfig,"localhost");
        //从连接池中获取单个连接
        Jedis jedis=pool.getResource();
        //如果需要密码
        //jedis.auth("password");
        return pool;
    }
}

使用spring调用redis

写在application.xml中<beans>里面

<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="paasword"/> -->
		<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>

假设有实体类Role,在方法中调用,使用Redistemplate保存role对象

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());

使用sessioncallback接口,多个命令放在同一个redis连接中执行(采用的是匿名类形式)

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<Role>() {
			@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());



猜你喜欢

转载自blog.csdn.net/MrMahz/article/details/80483549