redis开发测试简易代码

import com.google.common.collect.Sets;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author song
 * @version 1.0
 */

@RestController
@RequestMapping(value = "/myRedis")
@Slf4j
public class RedisController implements BaseController {

  private final StringRedisTemplate thisClient;

  public RedisController(StringRedisTemplate stringRedisTemplate) {
    this.thisClient = stringRedisTemplate;
  }

  @Value("${customize.redis.devops.passwd:}")
  private String myPasswd;

  @PostMapping("/get")
  public Object get(@RequestBody getParam getParam) {
    if (check(getParam)) {
      return thisClient.opsForValue().get(getParam.key);
    } else {
      log.error("attentionSomebody,wantToGet,{}", getParam);
      return null;
    }
  }

  @PostMapping(value = "/delByKeys")
  public void delByKeys(@RequestBody DelParam delParam) {
    if (check(delParam)) {
      thisClient.delete(delParam.keys);
    } else {
      log.error("attentionSomebody,wantToDel,{}", delParam);
    }
  }

  @PostMapping(value = "/scan")
  public Set scan(@RequestBody ScanParam scanParam) {
    if (check(scanParam)) {
      return thisClient.execute((RedisCallback>) connection -> {
        Set keysTmp = Sets.newHashSet();
        ScanOptions scanOptions = ScanOptions.scanOptions().match(scanParam.pattern)
            .count(scanParam.count).build();

        Cursor cursor = connection.scan(scanOptions);
        while (cursor.hasNext()) {
          keysTmp.add(new String(cursor.next()));
          if (!scanParam.all) {
            break;
          }
        }
        cursor.close();
        return keysTmp;
      });

    } else {
      log.error("attentionSomebody,wantToScan,{}", scanParam);
      return null;
    }
  }

  private  boolean check(T passwdParam) {
    if (StringUtils.isBlank(myPasswd)) {
      return false;
    }
    return Objects.equals(passwdParam.getPasswd(), myPasswd);
  }

  @Getter
  @Setter
  private static class ScanParam extends Passwd {

    private String pattern;
    private Long count = 200L;
    private boolean all = false;
  }

  @Getter
  @Setter
  private static class DelParam extends Passwd {

    private List keys;
  }

  @Getter
  @Setter
  private static class getParam extends Passwd {

    private String key;
  }

  @Getter
  @Setter
  private static class Passwd extends BaseBean {

    private String passwd;
  }

public static class BaseBean implements Serializable {
...
}
}

你可能感兴趣的:(redis,spring,java)