视频直播源码,采用Redis实现购物车功能
视频直播源码,采用Redis实现购物车功能
1 Redis核心配置类
package com.jmh.springboot03.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
/**
* @author 蒋明辉
* @data 2022/10/1 17:08
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
//实例化一个redis模板
RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
//设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//针对string类型的key和value进行序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//针对has类型的key和value进行序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//将上诉代码启用
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
2 定义购物车接口
IRedisService接口定义
package com.jmh.shopping.service;
import com.jmh.shopping.model.ShopCarItem;
import com.jmh.shopping.model.User;
import com.jmh.shopping.utils.JsonResponseBody;
import java.util.List;
/**
* @author 蒋明辉
* @data 2022/11/20 6:59
*/
public interface IRedisService {
/**
* 根据用户编号获取购物车商品
*/
List<ShopCarItem> queryRedisUserId(String id);
/**
* 增加购物车商品
*/
JsonResponseBody<?> add(User user, Long gid,Integer num);
/**
* 更新购物车商品
*/
JsonResponseBody<?> update(User user, Long gid, Integer num);
/**
* 删除购物车商品
*/
JsonResponseBody<?> delete(User user, String ids);
/**
* 获取用户要结算购物车商品信息
*/
List<ShopCarItem> toOrder(User user,String ids);
}
以上就是视频直播源码,采用Redis实现购物车功能, 更多内容欢迎关注之后的文章