为何要用缓存服务器以及在 Java 中实现一个 redis 缓存服务

缓存服务的意义

为何要使用缓存?说究竟是为了提升系统的运行速度。将用户频繁访问的内容存放在离用户最近,访问速度最快的地方,提升用户的响应速度。一个 web 应用的简单结构以下图。 java

web 应用典型架构

在这个结构中,用户的请求经过用户层来到业务层,业务层在从数据层获取数据,返回给用户层。在用户量小,数据量不太大的状况下,这个系统运行得很顺畅。可是随着用户量愈来愈大,数据库中的数据愈来愈多,系统的用户响应速度就愈来愈慢。系统的瓶颈通常都在数据库访问上。这个时候可能会将上面的架构改为下面的来缓解数据库的压力。 git

一主多从结构

在这个架构中,将数据库的读请求和写请求进行分离。数量众多的读请求都分配到从数据库上,主数据库只负责写请求。从库保持主动和主库保持同步。这个架构比上一个有了很大的改进,通常的互联网应用。这个架构就可以很好的支持了。他的一个缺点是比较复杂,主从库之间保持高效实时,或者准实时的同步是一个不容易作到的事情。因此咱们有了另外一个思路,采用一个缓存服务器来存储热点数据,而关系数据用来存储持久化的数据。结构以下图所示github

采用缓存服务器读的架构

采用缓存服务器读的架构

在这个架构中,当读取数据的时候,先从缓存服务器中获取数据,若是获取调,则直接返回该数据。若是没有获取调,则从数据库中获取数据。获取到后,将该数据缓存到换出数据库中,供下次访问使用。当插入或者更新数据的时候,先将数据写入到关系数据库中,而后再更新缓存数据库中的数据。web

固然了,为了应付更大规模的访问量,咱们还能够将上面两个改进的架构组合起来使用,既有读写分离的关系数据库,又有能够高速访问的缓存服务。redis

以上缓存服务器架构的前提就是从缓存服务器中获取数据的效率大大高于从关系型数据库中获取的效率。不然缓存服务器就没有任何意义了。redis 的数据是保存在内存中的,可以保证从 redis 中获取数据的时间效率比从关系数据库中获取高出不少。数据库


基于 redis 缓存服务的实现

这一章节用一个实例来讲明如何来在 Java 中实现一个 redis 的缓存服务。该代码是在上一篇文章 《在 Java 中使用 redis》 中实现的代码基础上增长完成的。代码同步发布在 GitHub 仓库apache

创建 maven 工程并引入依赖

参考文章 《在 Java 中使用 redis》 中的 pom.xml 文件内容数组

定义接口类com.x9710.common.redis.CacheService

在这个接口类中,主要定了下面的接口缓存

  • void putObject(String key, Object value);服务器

  • void putObject(String key, Object value, int expiration);

  • Object pullObject(String key);

  • Long ttl(String key);

  • boolean delObject(String key);

  • boolean expire(String key, int expireSecond);

  • void clearObject(); 这些接口分别用于存储不过时的对象存储未来过时对象获取缓存对象获取缓存对象剩余存活时间删除缓存对象设置缓存对象过时时间清除全部缓存对象的功能

    package com.x9710.common.redis;

    /**

    • 缓存服务接口
    • @author 杨高超
    • @since 2017-12-09 */ public interface CacheService {

    /**

    • 将对象存放到缓存中
    • @param key 存放的key
    • @param value 存放的值 */ void putObject(String key, Object value);

    /**

    • 将对象存放到缓存中
    • @param key 存放的key
    • @param value 存放的值
    • @param expiration 过时时间,单位秒 */ void putObject(String key, Object value, int expiration);

    /**

    • 从缓存中获取对象
    • @param key 要获取对象的key
    • @return 若是存在,返回对象,不然,返回null */ Object pullObject(String key);

    /**

    • 给缓存对象设置过时秒数
    • @param key 要获取对象的key
    • @param expireSecond 过时秒数
    • @return 若是存在,返回对象,不然,返回null */ boolean expire(String key, int expireSecond);

    /**

    • 获取缓存对象过时秒数
    • @param key 要获取对象的key
    • @return 若是对象不存在,返回-2,若是对象没有过时时间,返回-1,不然返回实际过时时间 */ Long ttl(String key);

    /**

    • 从缓存中删除对象
    • @param key 要删除对象的key
    • @return 若是出现错误,返回 false,不然返回true */ boolean delObject(String key);

    /**

    • 从缓存中清除对象 */

    void clearObject(); }

定义序列号辅助类com.x9710.common.redis.SerializeUtil

全部要保存到 redis 数据库中的对象须要先序列号为二进制数组,这个类的做用是将 Java 对象序列号为二级制数组或者将二级制数组反序列化为对象。

package com.x9710.common.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 对象序列化工具类
 *
 * @author 杨高超
 * @since 2017-10-09
 */
public class SerializeUtil {

/**
 * 将一个对象序列化为二进制数组
 *
 * @param object 要序列化的对象,该必须实现java.io.Serializable接口
 * @return 被序列化后的二进制数组
 */
public static byte[] serialize(Object object) {

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        return baos.toByteArray();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * 将一个二进制数组反序列化为一个对象。程序不检查反序列化过程当中的对象类型。
 *
 * @param bytes 要反序列化的二进制数
 * @return 反序列化后的对象
 */
public static Object unserialize(byte[] bytes) {
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}
复制代码

实现 redis 缓存服务类 com.x9710.common.redis.impl.CacheServiceRedisImpl

package com.x9710.common.redis.impl;

import com.x9710.common.redis.CacheService;
import com.x9710.common.redis.RedisConnection;
import com.x9710.common.redis.SerializeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;

/**
 * 缓存服务 redis 实现类
 *
 * @author 杨高超
 * @since 2017-12-09
 */
public class CacheServiceRedisImpl implements CacheService {
private static Log log = LogFactory.getLog(CacheServiceRedisImpl.class);

private RedisConnection redisConnection;

private Integer dbIndex;


public void setRedisConnection(RedisConnection redisConnection) {
    this.redisConnection = redisConnection;
}

public void setDbIndex(Integer dbIndex) {
    this.dbIndex = dbIndex;
}

public void putObject(String key, Object value) {
    putObject(key, value, -1);
}

public void putObject(String key, Object value, int expiration) {

    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        if (expiration > 0) {
            jedis.setex(key.getBytes(), expiration, SerializeUtil.serialize(value));
        } else {
            jedis.set(key.getBytes(), SerializeUtil.serialize(value));
        }
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }

}


public Object pullObject(String key) {

    log.trace("strar find cache with " + key);
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        byte[] result = jedis.get(key.getBytes());
        if (result == null) {
            log.trace("can not find caceh with " + key);
            return null;
        } else {
            log.trace("find cache success with " + key);
            return SerializeUtil.unserialize(result);
        }
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }

    return null;
}

public boolean expire(String key, int expireSecond) {
    log.trace("strar set expire " + key);
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        return jedis.expire(key, expireSecond) == 1;
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return false;
}

public Long ttl(String key) {
    log.trace("get set expire " + key);
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        return jedis.ttl(key);
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return -2L;
}

public boolean delObject(String key) {
    log.trace("strar delete cache with " + key);
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        return jedis.del(key.getBytes()) > 0;
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }

    return false;
}

public void clearObject() {

    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        jedis.flushDB();
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
}
复制代码

编写测试用例

package com.x9710.common.redis.test;

import com.x9710.common.redis.RedisConnection;
import com.x9710.common.redis.impl.CacheServiceRedisImpl;
import com.x9710.common.redis.test.domain.Student;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * 缓存服务测试类
 *
 * @author 杨高超
 * @since 2017-12-09
 */
public class RedisCacheTest {
private CacheServiceRedisImpl cacheService;

@Before
public void before() {
    RedisConnection redisConnection = RedisConnectionUtil.create();
    cacheService = new CacheServiceRedisImpl();
    cacheService.setDbIndex(2);
    cacheService.setRedisConnection(redisConnection);
}

@Test
public void testStringCache() {
    String key = "name";
    String value = "grace";
    cacheService.putObject(key, value);
    String cachValue = (String) cacheService.pullObject(key);
    //检查从缓存中获取的字符串是否等于原始的字符串
    Assert.assertTrue(value.equals(cachValue));
    //检查从缓存删除已有对象是否返回 true
    Assert.assertTrue(cacheService.delObject(key));
    //检查从缓存删除已有对象是否返回 false
    Assert.assertFalse(cacheService.delObject(key + "1"));
    //检查从缓存获取已删除对象是否返回 null
    Assert.assertTrue(cacheService.pullObject(key) == null);
}


@Test
public void testObjectCache() {
    Student oriStudent = new Student();
    oriStudent.setId("2938470s9d8f0");
    oriStudent.setName("柳白猿");
    oriStudent.setAge(36);
    cacheService.putObject(oriStudent.getId(), oriStudent);
    Student cacheStudent = (Student) cacheService.pullObject(oriStudent.getId());
    Assert.assertTrue(oriStudent.equals(cacheStudent));
    Assert.assertTrue(cacheService.delObject(oriStudent.getId()));
    Assert.assertTrue(cacheService.pullObject(oriStudent.getId()) == null);
}

@Test
public void testExpireCache() {
    String key = "name";
    String value = "grace";
    cacheService.putObject(key, value);
    cacheService.expire(key, 300);
    String cachValue = (String) cacheService.pullObject(key);
    Assert.assertTrue(value.equals(cachValue));
    Long ttl = cacheService.ttl(key);
    Assert.assertTrue(ttl > 250 && ttl <= 300);
    Assert.assertTrue(value.equals(cachValue));
    Assert.assertTrue(cacheService.delObject(key));
}
}
复制代码

测试结果

测试结果

redis 做为缓存服务是一个最基本的用法。这里只实现了基于 k-value 数据的缓存。其他的 Hash、Set、List 等缓存的用法大同小异。

后面我还将讲述 redis 实现分布式锁、全局惟一标识、LBS 服务和消息队列的服务。

对应的代码发布到了 GitHub

原文发表在简书中,原始连接

相关文章
相关标签/搜索