All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.huifer.crud.operation.RedisHashKeyOperation Maven / Gradle / Ivy

package com.github.huifer.crud.operation;

import com.google.gson.Gson;
import org.springframework.data.redis.core.StringRedisTemplate;

public abstract class RedisHashKeyOperation {

  Gson gson = new Gson();
  private StringRedisTemplate redisTemplate;

  protected void update(String id, T t) {

    T redisObj = this.byId(id);
    if (redisObj != null) {

      // 如果是redis中的类型和当前传入的类型相同
      if (redisObj.getClass().equals(t.getClass())) {
        this.insert(id, t);
      }
    }
  }

  protected void insert(String id, T t) {
    redisTemplate.opsForHash().put(key(), id, gson.toJson(t));
  }

  protected T byId(String id) {
    String o = (String) redisTemplate.opsForHash().get(key(), id);
    return (T) gson.fromJson(o, type());
  }

  protected void delete(String id) {
    this.redisTemplate.opsForHash().delete(key(), id);
  }

  protected Class type() {
    throw new RuntimeException("clazz is null");
  }

  protected String key() {
    throw new RuntimeException("key is null");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy