使用redis作为缓存

本文介绍了如何在公司项目中利用Redis作为缓存,以提升产品性能。在基于Ibatis和Ibator的框架下,通过Key Class作为缓存命名空间,Record Class继承Key Class,实现不同领域对象的区分。这种做法降低了开发难度,简化了缓存的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



公司要对部分产品使用redis缓存,来提高速度。根据公司现有框架,公司使用ibatis框架,并使用ibator自动化工具生产领域对象。

在使用ibator生产的所有对象,都会有Key Class类。

Key class will contain properties for each field in the primary key of a table. The property names will be generated automatically by Ibator.

Recorld Class类是实际的领域对象,并继承Key Class对象。 所以,我使用Key Class对象作为redis缓存的命名空间,来区分不同的领域对象。在获取Record Class时,使用Key Class作为查询对象。在使用过程中,减少了开发人员的难度,并简化了缓存的实现。

public class UserKey {
	private int ID;

	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
}
public class User extends UserKey {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [name=" + name + "]";
	}
}


public class JedisCacheTemplate {
	private Jedis jedis;
	private ObjectMapper mapper = new ObjectMapper();

	public JedisCacheTemplate() {
		this(new Jedis("localhost"));

	}

	public JedisCacheTemplate(Jedis jedis) {
		this.jedis = jedis;
	}
    //直接通过数据库表映射对象来进行缓存数据
	public void setObject(User user) {
		Class<? extends User> clazz = user.getClass();
		String namespaces = clazz.getSimpleName();
		Class<?> superclazz = clazz.getSuperclass();
		Method[] methods = superclazz.getDeclaredMethods();
		Map<Object, Object> prefixobj = new HashMap<Object, Object>();
		try {
			for (Method md : methods) {
				String mdname = md.getName();
				Type retureType = md.getGenericReturnType();
				if (!retureType.equals(Void.TYPE)) {
					Object value = md.invoke(user, null);
					prefixobj.put(mdname, value);
				}
			}
			//根据父类主键来作为redis键的命名空间
			String prefix = mapper.writeValueAsString(prefixobj);
			String value = mapper.writeValueAsString(user);
			jedis.set(namespaces + prefix, value);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

	}
    //根据ibator自动生成的主键对象来获取制定的对象
	public Object getObject(Object key, Class clazz) {
		String namespaces = clazz.getSimpleName();
		Map<Object, Object> prefixobj = new HashMap<Object, Object>();
		Method[] methods = key.getClass().getDeclaredMethods();
		try {
			for (Method md : methods) {
				String mdname = md.getName();
				Type retureType = md.getGenericReturnType();
				if (!retureType.equals(Void.TYPE)) {
					Object value = md.invoke(key, null);
					prefixobj.put(mdname, value);
				}
			}
			//来生成redis键的命名空间
			String prefix = mapper.writeValueAsString(prefixobj);
			String objectString = jedis.get(namespaces + prefix);
			Object obj = mapper.readValue(objectString, clazz);
			return obj;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public static void main(String[] args) {
		JedisCacheTemplate jedisCache = new JedisCacheTemplate();
		User user = new User();
		user.setID(1);
		user.setName("username");
		//设置缓存
		jedisCache.setObject(user);
		
		//从缓存中获取制定对象
		UserKey key = new UserKey();
		key.setID(1);
		User u = (User) jedisCache.getObject(key, User.class);
		System.out.println(u);
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值