package com.wechat.global.base.dao;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable> {

    @Autowired
    protected RedisTemplate<K, V> redisTemplate;

    /**
     * 设置redisTemplate
     * 
     * @param redisTemplate the redisTemplate to set
     */
    public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 获取 RedisSerializer <br>
     * ------------------------------<br>
     */
    protected RedisSerializer<String> getRedisSerializer() {
        return redisTemplate.getStringSerializer();
    }

    public void insert(K tableKey, String dataKey, V v) {
        redisTemplate.opsForHash().put(tableKey, dataKey, v);
    }

    public void update(K tableKey, String dataKey, V v) {
        redisTemplate.opsForHash().put(tableKey, dataKey, v);
    }

    public void delete(K tableKey, String dataKey) {
        redisTemplate.opsForHash().delete(tableKey, dataKey);
    }

    @SuppressWarnings("unchecked")
    public V selectOne(K tableKey, String dataKey) {
        return (V) redisTemplate.opsForHash().get(tableKey, dataKey);
    }
}