• 待办事项-redis


    package com.easyagu.liwei.util;

    import redis.clients.jedis.BinaryClient;
    import redis.clients.jedis.Jedis;

    import java.util.List;
    import java.util.Random;

    /**
    * todo
    */
    public class TodoDemo {
    /**
    * @param args
    */
    public static void main(String[] args) {
    TodoDemo todoDemo = new TodoDemo();
    Long userid = 12l;
    for (int i = 0; i < 20; i++) {
    todoDemo.addTodoEvent(userid, "第" + (i + 1) + "个待办事项");
    }
    //查询第一页待办事项
    int pageNo = 1;
    int pageSize = 10;
    List<String> todoEventPage = null;
    todoEventPage = todoDemo.findTodoEventByPage(userid, pageNo, pageSize);
    for (String todoEvent : todoEventPage) {
    System.out.println(todoEvent);
    }
    //插入一个待办事项
    Random random = new Random();
    int index = random.nextInt(todoEventPage.size());
    String targetTodoEvent = todoEventPage.get(index);
    //为啥我插入的待办事项不成功?????
    todoDemo.insertTodoEvent(userid, BinaryClient.LIST_POSITION.BEFORE, targetTodoEvent, "插入的待办事项000");
    //重新分页查询第一页代办事项
    todoEventPage = todoDemo.findTodoEventByPage(userid, pageNo, pageSize);
    for (String todoEvent : todoEventPage) {
    System.out.println(todoEvent);
    }
    //修改待办事项
    int index2= random.nextInt(todoEventPage.size());
    todoDemo.updateTodoEvent(userid,index2,"修改的数据");

    //完成一个待办事项
    todoDemo.finishTodoEvent(userid,todoEventPage.get(0));
    //最后查询一次待办事项
    todoEventPage = todoDemo.findTodoEventByPage(userid, pageNo, pageSize);
    for (String todoEvent : todoEventPage) {
    System.out.println(todoEvent);
    }


    }

    private Jedis jedis = new Jedis("127.0.0.1");


    public void addTodoEvent(long userId, String todoEvent) {
    jedis.lpush("todo_event::" + userId, todoEvent);
    }

    ;

    /**
    * 修改待办事项
    *
    * @param userId
    * @param index
    * @param updatedTodoEvent
    */
    public void updateTodoEvent(long userId, int index, String updatedTodoEvent) {
    jedis.lset("todo_event::" + userId, index, updatedTodoEvent);
    }

    /**
    * 完成待办事项
    *
    * @param userId
    * @param todoEvent
    */
    public void finishTodoEvent(long userId, String todoEvent) {
    jedis.lrem("todo_event::" + userId, 0, todoEvent);
    }

    ;


    /**
    * 插入待办事项
    *
    * @param userId
    * @param position
    * @param todoEvent
    * @param targetTodoEvent
    */
    public void insertTodoEvent(long userId, BinaryClient.LIST_POSITION position, String todoEvent, String targetTodoEvent) {
    jedis.linsert("todo_event::" + userId, position, targetTodoEvent, todoEvent);
    }

    ;


    /**
    * 分页查询待办事项
    *
    * @param userId
    * @param pageNo
    * @param pageSize
    * @return
    */
    public List<String> findTodoEventByPage(Long userId, Integer pageNo, Integer pageSize) {
    int startIndex = (pageNo - 1) * pageSize;
    int endIndex = pageNo * pageSize - 1;
    //返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
    return jedis.lrange("todo_event::" + userId, startIndex, endIndex);

    }
    }
  • 相关阅读:
    MongoDB
    Flask-Migrate
    Flask-Script
    Flask-SQLAlchemy
    SQLAlchemy
    DBUtils
    依存句法分析
    如何将本地的jar包上传到maven本地仓库中
    git使用手册
    中文网页编解码问题
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14630347.html
Copyright © 2020-2023  润新知