redis后端缓存优化编码

package com.jsh.dao.cache;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.jsh.model.po.Material;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

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

/**
 * @author guoli
 * @Time 2019年2月28日 下午2:41:38
 * @Description:TODO
 * @version 1.0
 */
public class RedisDao {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private final JedisPool jedisPool;

	private RuntimeSchema<Material> schema = RuntimeSchema.createFrom(Material.class);

	public RedisDao(String ip, int port) {
		jedisPool = new JedisPool(ip, port);
	}

	public Material getMaterial(long materialId) {
		// redis 操作逻辑
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "material:" + materialId;
				// 并没有实现内部的序列化操作
				// get-byte[]-反序列化-object
				// 采用自定义序列化protostuff
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					// 空对象
					Material material = schema.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, material, schema);// 作用material
																		// 反序列化,更节省空间
					return material;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return null;
	}

	public String putMaterial(Material material) {
		// set object-->序列化---> bytep[]
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "material:" + material.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(material, schema,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				// 超时缓存
				int timeout = 60 * 60;// 一个小时
				return jedis.setex(key.getBytes(), timeout, bytes);
			} finally {
				jedis.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
}

业务层代码

public void exportMaterial(long id){
		//优化缓存
//		1.访问redis
		Material material=redisDao.getMaterial(id);
		if(material==null){
			//访问数据库
			material=materialService.get(model.getMaterialID());
			if(material!=null){
				//存入redis
				redisDao.putMaterial(material);
			}
			
		}
	}

redis和protostuff

    <dependency>
        	<groupId>redis.clients</groupId>
        	<artifactId>jedis</artifactId>
        	<version>2.7.3</version>
        </dependency>
        <!-- protostuff 序列依赖 -->
        <dependency>
        	<groupId>com.dyuproject.protostuff</groupId>
        	<artifactId>protostuff-core</artifactId>
        	<version>1.0.8</version>
        </dependency>
        <dependency>
        	<groupId>com.dyuproject.protostuff</groupId>
        	<artifactId>protostuff-runtime</artifactId>
        	<version>1.0.8</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值