opsForList操作List集合
push操作分為leftpush和rightpush,其中leftpush是在list的左側添加,即列表的頭部,right是在list的左側添加,即在列表的尾部。可以根據業務酌情選擇。
pop操作也分為left和right,意思和push一樣。pop是獲取一個元素,並且刪除這個元素。
如果只想要查看某個元素。可以使用range,他有三個參數,第一個參數是key,后面是搜索范圍,全集合搜索可以用(key,0,-1);
有時候希望給添加的緩存設置生命時間,到期后自動刪除該緩存。可以使用
stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
key是鍵,需要是已經存在的,seconds是時間,單位是long類型,最后一個參數單位。
時間可以使用java8的語法。
Date currentDate = new Date(); LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault()).plusHours(2); LocalDateTime currentDateTime =LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault()); long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
LocalDateTime中的plusHours是指當前時候多少小時之后,同類型的還有plusSeconds,plusMinutes(minutes)等等;
別的還有withHours是直接賦值時間。