1:Redis学习初级阶段,仅将redis如何存取List<Object>的简单实例分享如下。
2
(1) 修改配置文件application-redis.xml中的 redisTemplate 的属性, 添加该配置的作用是为了存储实体对象,将实体对象序列化后存储,否则就只能存储String的对象。
<property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property>
(2)新增需要的实体类对象Address,该实体类必须实现序列化接口,目的嘛,你懂的。
/** * Address 地址实体类 * @author zzs * */ public class Address implements Serializable { /** * Address serialVersionUID */ private static final long serialVersionUID = 6654868162665356297L; private String id; private String name; private String userId;//关联User的id public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
(3)新增接口 IRedisSetAndListDao
/** * Redis 的Set和List操作 * @author zzs * */ public interface IRedisSetAndListDao { /** * 新增addrList数据到内存库 * @param addrList * @return */ public long addListAddress(List<Address> addrList); }
(4)新增实现接口 RedisSetAndListDao
public class RedisSetAndListDao extends AbstractBaseRedisDao<String, Address > implements IRedisSetAndListDao { /** * 新增addrList数据到内存库 * @param addrList * @return */ public long addListAddress(List<Address> addrList) { Address[] addrArray = new Address[addrList.size()]; for( int i = 0 ; i < addrList.size(); i++ ) { addrArray[i] = addrList.get(i); } long i = redisTemplate.opsForList().leftPushAll("addrList", addrArray); return i; } }
(5)在application-redis.xml添加接口的注入配置
<bean id="redisSetAndListDao" class="redis.dao.impl.RedisSetAndListDao" />
(6)在你的测试类里面新增测试方法addListAddress
/** * 新增addrList数据到内存库 * @param addrList * @return */ @Test public void addListAddress() { List<Address> addrList = new ArrayList<Address>(); for( int i = 0; i < 8; i++ ){ Address address = new Address(); address.setId("address"+System.currentTimeMillis()); address.setName("zhangzhisheng"+System.currentTimeMillis()); address.setUserId("userId"+System.currentTimeMillis()); addrList.add(address); } Long count = redisSetAndListDao.addListAddress(addrList); }
下次再见。
相关推荐
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect....
RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 使用protostuff序列化 ProtostuffSerializer serializer = new ProtostuffSerializer(); ...
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, ...
<artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> <!-- 注意:此版本兼容Spring 4.2.5和Redis 2.8 --> </dependency> <dependency> <groupId>org.apache.commons</groupId> ...
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, ...
<artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 接下来,我们需要配置Redis连接...
List<Object> results = tx.exec(); ``` 这个例子展示了如何在一个事务中执行两个设置键值的操作,确保它们要么全部成功,要么全部失败。 总结来说,Spring Boot结合Jedis提供了一种简单、高效的方式去集成和操作...
Map<String, String> map = new HashMap<>(); map.put("field1", "value1"); jedis.hset("hashKey", map); Map<String, String> result = jedis.hgetAll("hashKey"); ``` - **列表操作**:插入和获取元素。 ...
public List<MyEntity> testCache() { return repository.findAll(); } } ``` 通过以上步骤,我们就成功地在Spring Boot 2.1.4.RELEASE项目中配置了使用Redis作为Hibernate二级缓存的环境。这将显著提升数据库...
List<Object> results = jedis.exec(); System.out.println(results); // 输出 [null, null] ``` 7. 消息订阅/发布(Pub/Sub): Redis还提供了消息发布/订阅功能,允许不同客户端间通信: ```java // 发布...
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); // 可能需要进一步配置序列化器、key策略等 ``` 4. **注册到Spring Bean**: 将自定义的...
List<Object> results = tx.exec(); ``` 6. **发布订阅**: Jedis也支持Redis的发布订阅功能,可以实现消息的发布和接收。 ```java JedisPubSub subscriber = new MySubscriber(); jedis.subscribe...
RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 可以进一步配置序列化器、key、value工厂等 return template; } ``` 至于Repository,Spring ...
List<Object> results = jedis.exec(); ``` 发布订阅可以用于实现简单的消息传递: ```java jedis.subscribe(new JedisPubSub() { @Override public void onMessage(String channel, String message) { System....
List<Object> results = jedis.exec(); ``` 在分布式应用中,可以使用Sentinel或Cluster来实现高可用性和负载均衡。Sentinel监控Redis主从集群,当主节点故障时自动进行故障切换。而Redis Cluster则提供数据分片和...
listProfit = new ArrayList<>(); for (Map map : mapList) { Map<String, Object> sObj = (Map<String, Object>) map; listProfit.add(sObj); } } ``` 此外,对于更复杂的泛型对象,如`List<User>`,我们可以...
Map<String, String> hash = new HashMap<>(); hash.put("field1", "value1"); hash.put("field2", "value2"); jedis.hset("myHash", hash); Map<String, String> result = jedis.hgetAll("myHash"); ``` 5. ...
List<Object> responses = jedis.exec(); ``` 在这个例子中,`multi`开始一个事务,接着的命令会被放入队列,最后`exec`执行所有命令。 为了释放资源,使用完Jedis实例后别忘了关闭它: ```java jedis.close(); `...