`

使用 redisTemplate

 
阅读更多
使用 redisTemplate(spring-data-redis)操作redis数据库

课程配置参数CourseParamCofig

/**
 * 课程参数配置
 * Created by lgq on 2015/7/1.
 */
public class CourseParamConfig {


    public static final String REDIS_KEY = "CourseParamConfig";

    /**
     * 企业
     */
    private String companyId;

    /**
     * 课程发布是否需要审核
     */
    private boolean audit = Boolean.FALSE;

    /**
     * 学员看课的时候是否允许快进
     */
    private boolean fastForward = Boolean.TRUE;


    public boolean isAudit() {
        return audit;
    }

    public void setAudit(boolean audit) {
        this.audit = audit;
    }

    public boolean isFastForward() {
        return fastForward;
    }

    public void setFastForward(boolean fastForward) {
        this.fastForward = fastForward;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

}


操作redis数据库,添加,删除和查询对象信息

/**
 * Created by lgq on 2015/7/17.
 */
public class CourseParamConfigManager {

    // reidsTemplate 对象
    private static RedisTemplate<String, String> redisTemplate = new RedisFactory<String,String>().redisTemplateRegistry(RedisDataType.CONFIGURATION);

    // 向redis中 存储数据
    public static void put(CourseParamConfig config) {
        redisTemplate.opsForHash().put(CourseParamConfig.REDIS_KEY, config.getCompanyId(), JSON.toJSONString(config));
    }

    // 删除redis中的数据
    public static void delete(String companyId) {
        redisTemplate.opsForHash().delete(CourseParamConfig.REDIS_KEY, companyId);
    }

    // 获取redis中的数据
    public static CourseParamConfig get(String companyId) {
        String configInfo = (String)redisTemplate.boundHashOps(CourseParamConfig.REDIS_KEY).get(companyId);
        CourseParamConfig courseParamConfig = JSON.parseObject(configInfo,new TypeReference<CourseParamConfig>() {});
        return courseParamConfig;
    }


    public static void main(String[] args) {

        CourseParamConfigManager courseParamConfigManager = new CourseParamConfigManager();
        CourseParamConfig courseParamConfig = new CourseParamConfig();
        courseParamConfig.setCompanyId("lgq");
        courseParamConfig.setAudit(false);
        courseParamConfig.setFastForward(true);
        // 1.
        courseParamConfigManager.put(courseParamConfig);
        // 2.
        CourseParamConfig configuration = courseParamConfigManager.get("lgq");
        // 3.
        courseParamConfigManager.delete("lgq");

    }
}


http://my.oschina.net/xiaoxiangdaizi/blog/491228

http://bbs.csdn.net/topics/391076367


http://www.cnblogs.com/jifeng/p/4676863.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics