redis simple data caching tools

Tools:

package io.renren.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: ivvdata-security
 * @description: redis工具类
 * @author: HYJ
 * @create: 2019-09-27 18:00
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 存储数据
     */
    public void set(String key, String value){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, value);
    }

    /**
     * 获取数据
     */
    public String get(String key){
        ValueOperations <String, String> OPS = stringRedisTemplate.opsForValue ();
         return ops.get (Key); 
    } 


    / ** 
     * set list to add data to the operator [] List 
     * @param Key List Name 
     * @param values stored value 
     * @return after completion of storage, the length of the current List
      * / 
    public Long leftPush (String Key, Object value) { 
        ListOperations <String, Object> = opsForList redisTemplate.opsForList (); 
        Long length = opsForList.leftPush (Key, value);
         return length; 
    } 


    / ** 
     * List to store a set of collection 
     *@param List Key operated 
     * @param List need to add List 
     * @return after storing is completed, the current length of List
      * / 
    public Long leftPushAll (String Key, List <Object> List) { 
        ListOperations <String, Object> = opsForList redisTemplate.opsForList ();
         return opsForList.leftPushAll (Key, list); 
    } 



    / ** 
     * Get List data set 
     * @param Key List name 
     * @return data stored in list
      * / 
    public List <Object>  Range (String Key) {
        ListOperations < String, Object> opsForList = redisTemplate.opsForList ();
         return opsForList.range (Key, 0, -1 ;) 
    } 


    / ** 
     * List of removing elements 
     * @param List Key operated 
     * @param number of elements to be deleted count [ , remove negative values from the tail to the head; on the contrary a positive number] 
     * @param element value value to be deleted 
     * @return after removal is completed, the current List of length
      * / 
    public Long the remove (String Key, Long cOUNT, Object value) { 
        ListOperations <String, Object> = opsForList redisTemplate.opsForList ();
         return opsForList.remove (Key, COUNT, value);

    } 


    / ** 
     * Set expiration time 
     * / 
    public  Boolean The expire (String Key, Long The expire) { 
        Boolean expire2 = stringRedisTemplate.expire (Key, The expire, TimeUnit.SECONDS);
         return expire2; 
    } 

    / ** 
     * delete data 
     * / 
    public  void Remove (String Key) { 
        stringRedisTemplate.delete (Key); 
    } 

    / ** 
     * increment operator 
     * @param Delta self-energizing step
      * / 
    public long INCREMENT (String Key, long Delta) {
         return stringRedisTemplate.opsForValue().increment(key,delta);
    }
}

 

Guess you like

Origin www.cnblogs.com/H-Dream/p/11763501.html