doujia7517 2013-12-25 12:42
浏览 40
已采纳

如何避免每次加载用户的徽标

I have a site that has a lot of images, like user's logo, product's logo/images or company's logo/images.

enter image description here

Now, I store these logos or images name in MySQL. If I want to know one user's logo, I must query mysql, and find the logo. Just like this:

<img src="{{FileManager::getFile('user_logo',$loop->photo)}}" />

The problem is , my site is growing and become bigger and bigger. Suppose, I have a page, which will show users's info (user's name, user's age... user's logo).
That I must query MySQL to find their logo. Everytime, this will cost lots of time waiting for page loading user's logo. I wonder how can I speed up loading logos/images ?

I am using Laravel 4 with this server specification:

  • Apache
  • PHP 5.4
  • MySQL 5.5
  • Redis 2.6
  • 写回答

3条回答 默认 最新

  • doudou3935 2013-12-25 12:51
    关注

    The idea

    The best way is to store the logo images in your servers hierarchy according to the user ids. So you don't need to query for the images because you know everything to get them:

    • The images place: Will be a fix folder.
    • The images name: The user's id.

    For example you can create an image directory where you can save all the logos with a filename that matches the user's id. In this way you can simply get the user logos through a simple URL like example.com/images/{userid}.

    If this doesn't fit for your directory structure just restructure it. For example you can create a folder for each user where you save their pictures with names like logo1, logo2 , etc or simply put this to a subfolder too. You could get those via an URL like example.com/images/{userid}/logo1 or example.com/images/{userid}/logos/1.

    Implementation guide

    Here are some implementation guides for the server side:

    User's profile page

    1. Get the user id from the URL. Like the id 150 from the URL example example.com/users/150.
    2. Validate it.
    3. Load the logo like: <img src="example.com/images/150/logos/1" alt="Logo">

    User's list page

    1. Query for all users like: SELECT id, name FROM users WHERE id > 50
    2. Render the list with the images using the user ids.

    An example loop for the list:

    foreach ($users as $user) {
        echo $user->name;
        echo '<br>';
        echo '<img src="example.com/images/' . $user->id . '/logos/1" alt="Logo">';
    }
    

    Every other list

    The same as the user's list implementation just make sure you store or join the users so you can get the id's from one query again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试