qq_43304875 2020-06-20 09:25 采纳率: 40%
浏览 181
已采纳

redis缓存与数据库表主键的一些疑惑。

我在学习redis缓存时发现如下疑惑。
存在一个表,代码如下:

create table user(
    id int(12) auto_increment not null,
    user_name varchar(60) not null,
    pwd varchar(60) not null,
    available int(1) default 1 check(available in(0, 1)),
    province_address varchar(10),
    city_address varchar(10),
    note varchar(256),
    primary key(id),
    unique(user_name)
)engine=InnoDB default charset=utf8;

这个表的主键是自增长的id,而且存在一个唯一键(user_name)。

对应的Mapper如下:

public interface BaseMapper<T, ID extends Serializable> {
    /**
     * delete the instance by primary key
     * @param id: the primary key of table
     * @return 1 means delete which 0 means not delete
     */
    public int deleteByPrimaryKey(ID id);

    /**
     * insert the instance to table
     * @param record: the instance of class T
     * @return 1 means insert which 0 means not insert
     */
    int insert(T record);

    /**
     * insert some column of instance into table
     * @param record
     * @return 1 means insert which 0 means not insert
     */
    int insertSelective(T record);

    /**
     * select the instance from table
     * @param id: the primary key of the table
     * @return the instance of class, if null means there is not this row
     */
    T selectByPrimaryKey(ID id);

    /**
     * update some column of the instance
     * @param record
     * @return 1 means update which 0 means not update
     */
    int updateByPrimaryKeySelective(T record);

    /**
     * update instance
     * @param record
     * @return 1 means update which 0 means not update
     */
    int updateByPrimaryKey(T record);

    /**
     * select all instance of table
     * @return
     */
    List<T> selectAll();
}

ps:代码摘自网上,来源就不标注了(当时没保存)。

redis缓存代码如下:

@Override
    @Cacheable(value="redisCache", key="'redis_user_'+#id")
    public User selectByPrimaryKey(Long id){
        return super.selectByPrimaryKey(id);
    }

    @Override
    @CachePut(value="redisCache", key="'redis_user_'+#result.id")
    public User insert(User record){
        return super.insert(record);
    }

    /**
     * 更新数据后更新缓存,如果condition配置项使结果返回为null,不缓存
     * @param record
     * @return
     */
    @Override
    @CachePut(value="redisCache",
            condition = "#result != 'null'",
            key="'redis_user_'+#record.id")
    public User updateByPrimaryKey(User record){
        // 为了避免脏读所以这里需要读取一下数据库
        // 因为自调用的问题,所以这里调用的selectByPrimaryKey(id)的缓存不会实现
        // 因此这里会执行sql
        User user = this.selectByPrimaryKey(record.getId());
        if(user == null){
            return null;
        }
        return super.updateByPrimaryKey(record);
    }

    @Override
    @CacheEvict(value="redisCache", key="'redis_user_'+#id",
            beforeInvocation = false)
    public boolean deleteByPrimaryKey(Long id){
        return super.deleteByPrimaryKey(id);
    }

根据代码我可以知道,我是用redisCache::redis_user_#id来作为键的名字的。
可是由于id是自增长的,所以我查询时,应该使用use_name来查询的,可是
用user_name来查询的话,那么我的redis缓存就没有用处了。
为了能够使用缓存我应该用user_name作为主键吗?
但是我在书上看到它的建表语句是用自增长的id作为主键的。
这样是不是用什么深意在里面?

  • 写回答

1条回答 默认 最新

  • Json-Huang 2020-06-20 10:37
    关注

    算是数据库设计规范吧,一般用id作为主键,在设计其它数据表会考虑设计id为其外键,很少会考虑用用户名(user_name)作为其它表外键(但也没有强制要求,要用也是可以)。

    user_name应该是唯一的,可以通过user_name查询id,然后在通过redis查询id对应的缓存即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)