Search in sources :

Example 1 with KeyAndValue

use of com.myblog.model.KeyAndValue in project newblogback by Zephery.

the class TagServiceImpl method updatetag.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Integer updatetag(Integer tId) {
    // 标签
    List<Tag> tags = tagMapper.getAllTags();
    JsonArray jsonArray = new JsonArray();
    for (Tag tag : tags) {
        List<Blog> blogs = tagMapper.getblogbytagid(tag.gettId());
        String str = tag.gettName() + " " + "(" + String.valueOf(blogs.size()) + ")";
        KeyAndValue keyAndValue = new KeyAndValue();
        keyAndValue.setKey(tag.gettName());
        keyAndValue.setValue(str);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("key", tag.gettId());
        jsonObject.addProperty("value", str);
        jsonArray.add(jsonObject);
    }
    boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

        @Override
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            connection.set(serializer.serialize("biaoqian"), serializer.serialize(jsonArray.toString()));
            return true;
        }
    });
    return tId;
}
Also used : KeyAndValue(com.myblog.model.KeyAndValue) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Blog (com.myblog.model.Blog)1 KeyAndValue (com.myblog.model.KeyAndValue)1 Tag (com.myblog.model.Tag)1 DataAccessException (org.springframework.dao.DataAccessException)1 RedisConnection (org.springframework.data.redis.connection.RedisConnection)1 RedisSerializer (org.springframework.data.redis.serializer.RedisSerializer)1