第一步:书写接口
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
}
}