redisTimple 对redis进行增加、删除、查找

4 篇文章 0 订阅

 配置redisConfig

package com.qn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * Created by win 10 on 2019/2/15.
 */
@Configuration
public class RedisConfig {

  @Bean
  public RedisTemplate<String, Object> redisTemplate(
      RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
  }

  @Bean
  public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
    return stringRedisTemplate;
  }

}

 可以直接操作,最好编辑为工具类

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yzf.cloud.model.User;



@RestController
public class RedisController {

	@Autowired
	private RedisTemplate redisTemplate;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@RequestMapping("/redis")
	public String getRedis(){
		stringRedisTemplate.opsForValue().set("key", "stringValue");
		return stringRedisTemplate.opsForValue().get("key");
	}
	
	@RequestMapping("/aa")
	public Object getRedisTemplate(){
		User user=new User();
		user.setAge(11);
		user.setName("redis");
		redisTemplate.opsForValue().set("key2", user);
		return redisTemplate.opsForValue().get("key2");
	}
	
	@PostMapping("/redisDemo")
	public  void redisDemo(){
		//添加一个 key 
	    ValueOperations<String, Object> value = redisTemplate.opsForValue();
	    value.set("lp", "hello word22");
	    value.set("lp2", "设置过期时间22233",10,TimeUnit.SECONDS);
	    //获取 这个 key 的值
	    System.out.println("value "+value.get("lp")+"  "+value.get("lp2"));
	    //添加 一个 hash集合
	    HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("name", "lp");
	    map.put("age", "26");
	    hash.putAll("lpMap", map);
	    //获取 map
	    System.out.println("hash"+hash.entries("lpMap"));
	    //添加 一个 list 列表
	    ListOperations<String, Object> list = redisTemplate.opsForList();
	    list.rightPush("lpList", "lp");
	    list.rightPush("lpList", "26");
	    //输出 list
	    System.out.println("list"+list.range("lpList", 0, 1));
	    //添加 一个 set 集合
	    SetOperations<String, Object> set = redisTemplate.opsForSet();
	    set.add("lpSet", "lp");
	    set.add("lpSet", "26");
	    set.add("lpSet", "178cm");
	    //输出 set 集合
	    System.out.println("set"+set.members("lpSet"));
	    //添加有序的 set 集合
	    ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
	    zset.add("lpZset", "lp", 0);
	    zset.add("lpZset", "26", 1);
	    zset.add("lpZset", "178cm", 2);
	    //输出有序 set 集合
	    System.out.println("zset"+zset.rangeByScore("lpZset", 0, 2));
	}
	
	@PostMapping("deleteRedis")
	public String deleteRedis(){
		redisTemplate.opsForValue().getOperations().delete("lp2");
		return "删除lp2成功";
	}
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值