springboot - redis

1.添加redis数据库(noSql的一种)依赖;

2.启动器上添加注解@EnableCaching;

3.redis启动器默认情况下会找本地的redis服务,端口号默认是6379;如果需要访问其他服务器的redis,则需要在配置文件中进行配置

4.配置redis所需配置的配置类

@Component

public class RedisRepository  {

    @Autowired

    private RedisTemplateredisTemplate;

    public RedisTemplateRedisRepository() {

        //使用StringRedisSerializer来序列化和反序列化redis的key值

        RedisSerializer stringSerializer =new StringRedisSerializer();

        redisTemplate.setKeySerializer(stringSerializer);

        redisTemplate.setValueSerializer(stringSerializer);

        redisTemplate.setHashKeySerializer(stringSerializer);

        redisTemplate.setHashValueSerializer(stringSerializer);

        redisTemplate.setStringSerializer(stringSerializer);

        // explicitly enable transaction support

        redisTemplate.setEnableTransactionSupport(true);

        return redisTemplate;

    }

    public BoundValueOperationsgetBoundValueOps(K key) {

        return redisTemplate.boundValueOps(key);

    }

    public BoundZSetOperationsgetBoundZSetOps(K key) {

        return redisTemplate.boundZSetOps(key);

    }

    public BoundSetOperationsgetBoundSetOps(K key) {

        return redisTemplate.boundSetOps(key);

    }

    public BoundListOperationsgetBoundListOps(K key) {

        return redisTemplate.boundListOps(key);

    }

    public BoundHashOperationsgetBoundHashOps(K key) {

        return redisTemplate.boundHashOps(key);

    }

    // Key

    public void del(final K key) {

        redisTemplate.delete(key);

    }

    public void del(final Collection keys) {

        redisTemplate.delete(keys);

    }

    public Booleanexists(final K key) {

        return redisTemplate.hasKey(key);

    }

    public Booleanexpire(final K key,final long timeout,final TimeUnit unit) {

        return redisTemplate.expire(key, timeout, unit);

    }

    public void expireAt(final K key,Date date) {

        redisTemplate.expireAt(key, date);

    }

    public Setkeys(final K pattern) {

        return redisTemplate.keys(pattern);

    }

    public Stringtype(final K key) {

        return redisTemplate.type(key).code();

    }

    public V get(final K key) {

    BoundValueOperations ops  =this.getBoundValueOps(key);

        return ops.get();

    }

    public V getSet(final K key,final V value) {

        BoundValueOperations ops  =this.getBoundValueOps(key);

        return ops.getAndSet(value);

    }

    public Longincr(final K key,final long delta) {

        BoundValueOperations ops  =this.getBoundValueOps(key);

        return ops.increment(delta);

    }

    public void set(final K key,final V value) {

        BoundValueOperations ops  =this.getBoundValueOps(key);

        ops.set(value);

    }

    public void set(final K key,final V value,final long timeout,final TimeUnit unit) {

        BoundValueOperations ops  =this.getBoundValueOps(key);

        ops.set(value, timeout, unit);

    }

    // Hash

    public void hDel(final K key,final Object... hKeys) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        ops.delete(hKeys);

    }

    public BooleanhExists(final K key,final K hKeys) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.hasKey(hKeys);

    }

    public MaphGet(final K key) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.entries();

    }

    public V hGet(final K key,final K hKey) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.get(hKey);

    }

    public SethKeys(final K key) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.keys();

    }

    public LonghLen(final K key) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.size();

    }

    public void hSet(final K key,final K hk, final V hv) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        ops.put(hk, hv);

    }

    public void hSet(final K key,final Map map) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        ops.putAll(map);

    }

    public ListhVals(final K key) {

        BoundHashOperations ops  =this.getBoundHashOps(key);

        return ops.values();

    }

    // List

    public V lIndex(final K key,final long index) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.index(index);

    }

    public void lInsert(final K key,final long index,V value) {

        BoundListOperations ops  =this.getBoundListOps(key);

        ops.set(index, value);

    }

    public LonglLen(final K key) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.size();

    }

    public V lPop(final K key) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.leftPop();

    }

   public V lPop(final K key,long timeout,TimeUnit unit) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.leftPop(timeout, unit);

    }

    public LonglPush(final K key,final V value) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.leftPush(value);

    }

    public ListlRange(final K key,final long start,final long end) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.range(start, end);

    }

    public LonglRem(final K key,final long index,final V value) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.remove(index, value);

    }

    public void lSet(final K key,final long index,final V value) {

        BoundListOperations ops  =this.getBoundListOps(key);

        ops.set(index, value);

    }

    public void ltrim(final K key,final long start,final long end) {

        BoundListOperations ops  =this.getBoundListOps(key);

        ops.trim(start, end);

    }

    public LongrPush(final K key,final V value) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.rightPush(value);

    }

    public V rPop(final K key) {

        BoundListOperations ops  =this.getBoundListOps(key);

        return ops.rightPop();

    }

    // Set

    public LongsAdd(final K key,final V value) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.add(value);

    }

    public SetsDiff(final K key) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.diff(key);

    }

    public SetsMembers(final K key) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.members();

    }

    public BooleansIsMember(final K key,final V value ){

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.isMember(value);

    }

    public V sPop(final K key) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.pop();

    }

    public LongsRem(final K key, final V value ) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.remove(value);

    }

    public LongsCard(K key) {

        BoundSetOperations ops  =this.getBoundSetOps(key);

        return ops.size();

    }

    // SortedSet

    public void zAdd(final K key,final V value,final double score) {

        BoundZSetOperations ops  =this.getBoundZSetOps(key);

        ops.add(value, score);

    }

   public SetzRange(final K key,final long start,final long end) {

        BoundZSetOperations ops  =this.getBoundZSetOps(key);

        return ops.range(start, end);

    }

    public LongzRem(final K key,final Object... values) {

        BoundZSetOperations ops  =this.getBoundZSetOps(key);

        return ops.remove(values);

    }

    public LongzCard(K key) {

        BoundZSetOperations ops  =this.getBoundZSetOps(key);

        return ops.zCard();

    }

    public RedisTemplategetRedisTemplate() {

        return redisTemplate;

    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {

        this.redisTemplate = redisTemplate;

    }

}

5.测试


6.查看效果

你可能感兴趣的:(springboot - redis)