java实现秒杀业务之通用缓存key封装(接口、抽象类、实现类的区别)

第一步:书写接口

package com.jack.seckill.redis;

public interface KeyPrefix {
	
	public int expireSeconds();
	
	public String getPrefix();
	

}

第二步:书写抽象类

package com.jack.seckill.redis;

public abstract class BasePrefix implements KeyPrefix {
	
	private int expireSeconds;
	
	private String prefix;
	
	public BasePrefix(String prefix) {//0代表永不过期
		this(0,prefix);
	}
	
	public BasePrefix(int expireSeconds, String prefix) {
		super();
		this.expireSeconds = expireSeconds;
		this.prefix = prefix;
	}
	public int expireSeconds() {
		return expireSeconds;
	}

	public String getPrefix() {
		String className=getClass().getSimpleName();
		return className+":"+prefix;
	}
	
	
	
	

}

 

第三步:书写实现类

UserKey:

package com.jack.seckill.redis;

public class UserKey extends BasePrefix{

	private UserKey(String prefix) {
		super(prefix);
	}
	public static UserKey getById=new UserKey("id");
	public static UserKey getByName=new UserKey("name");
}

 

OrderKey:

package com.jack.seckill.redis;

public class OrderKey extends BasePrefix{

	public OrderKey(int expireSeconds, String prefix) {
		super(expireSeconds, prefix);
		// TODO Auto-generated constructor stub
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值