Redis已在AppConfig中配置好, 在Controller中使用没有问题, 使用方法中在类中加入静态变量,如下:
public class Api4YMLPWController extends ApiController {
private static final Log log = Log.getLog(wxaApiController.class);
private Cache redis = Redis.use();
public void index(){
HashMap re = redis.get("xxx"); //正常
.....但是, 在Model中, 这样使用就会出现redis为空, 只能在各个方法中引入,如下:
public class Category extends Model<Category> {
public static final Category dao = new Category().dao();
//private static Cache redis = Redis.use(); Cache在这里定义,在方法中引用时为null
public Record findByTag(int cid, String tag){
Cache redis = Redis.use(); // Cache必须在方法中定义才能正常get
Record category = redis.get(tag+cid);
if(category==null){
category=Db.findFirst("select * from Category where _deleted_ is null and cid=? and tag=?", cid, tag);
if(category!=null)
redis.setex(tag + cid, 60, category);
}
return category;
}
...如果Model中多个方法需要使用Redis,每个方法中都要定义,请问有没有简洁的方法
项目:JFinal