Home>

1. Preparation

Routing and controller

The definition of the route and the creation of the controller remain the same as the creation of the restful style controller to achieve the addition, deletion, modification and review of the article.

Create data table

Regarding the article corresponding data table, we have used the query builder in the database section to implement advanced queries on the database.Here we can use the data table created earlier.

Create article model

The creation of the article model post is also consistent with the orm overview, model definition and basic query created in the previous eloquent orm section.

2.Modify the controller

Before, we implemented the addition, deletion, modification, and checking of articles through the cache.Here we modify it to add, delete, change and check through the database:

<?php
  namespace app \ http \ controllers;
  use illuminate \ http \ request;
  use cache;
  use app \ models \ post;
  use app \ http \ requests;
  use app \ http \ controllers \ controller;
  class postcontroller extends controller
  {
    /**
     * Show article list.
     *
     * @return response
     * /
    public function index ()
    {
      //Get all data using all,If the amount of data is large, use paging to obtain
      $posts=post ::all ();
      if (! $posts)
        exit ("No articles have been posted!");
      $html="<ul>";
      foreach ($posts as $post) {
        $html.="<li><a href =". route ("post.show", ["post" =>$post]). ">". $post- > title. "< ;/li>";
      }
      $html.="</ul>";
      return $html;
    }
    /**
     * Create new article form page
     *
     * @return response
     * /
    public function create ()
    {
      $posturl=route ("post.store");
      $csrf_field=csrf_field ();
      $html =<<create
        <form action="$posturl" method="post">
          $csrf_field
          <input type="text" name="title"><br /><br />
          <textarea name="content" cols="50" rows="5"></textarea><br /><br />
          <input type="submit" value="submit" />
        </form>
create;
      return $html;
}
    /**
     * Store newly created articles to memory
     *
     * @param request $request
     * @return response
     * /
    public function store (request $request)
    {
      $title=$request->input ("title");
      $content=$request->input ("content");
      $post=new post;
      $post->title=$title;
      $post->content=$content;
      $post->save ();
      return redirect ()->route ("post.show", ["post" =>$post]);
    }
    /**
     * Show specified articles
     *
     * @param int $id
     * @return response
     * /
    public function show ($id)
    {
      $post=cache ::get ("post _". $id);
      if (! $post) {
        $post=post ::find ($id);
        if (! $post)
          exit ("The specified article does not exist!");
        cache ::put ("post _". $id, $post, 60 * 24 * 7);
      }
      if (! cache ::get ("post_views _". $id))
        cache ::forever ("post_views _". $id, 0);
      $views=cache ::increment ("post_views _". $id);
      cache ::forever ("post_views _". $id, $views);
      $editurl=route ("post.edit", ["post" =>$post]);
      $deleteurl=route ("post.destroy", ["post" =>$post]);
      $html =<<post
        <h3>{$post->title}</h3>
        <p>{$post->content}</p>
        <i>{$views} people have read</i>
        <p>
          <a href="{$editurl}">Edit</a>
        </p>
post;
      return $html;
    }
    /**
     * Display the form page for editing the specified article
     *
     * @param int $id
     * @return response
     * /
    public function edit ($id)
    {
      $post=post ::find ($id);
      if (! $post)
        exit ("The specified article does not exist!");
      $posturl=route ("post.update", ["post" =>$post]);
      $csrf_field=csrf_field ();
      $html =<<create
        <form action="$posturl" method="post">
          $csrf_field
          <input type="hidden" name="_ method" value="put" />
          <input type="text" name="title" value="{$post->title}"><br /><br />
          <textarea name="content" cols="50" rows="5">{$post->content}&/textarea<br /><br />
          <input type="submit" value="submit" />
        </form>
create;
      return $html;
    }
    /**
     * Update specified articles in memory
     *
     * @param request $request
     * @param int $id
     * @return response
     * /
    public function update (request $request, $id)
    {
      $post=post ::find ($id);
      if (! $post)
        exit ("The specified article does not exist!");
      $title=$request->input ("title");
      $content=$request->input ("content");
      $post->title=$title;
      $post->content=$content;
      $post->save ();
      return redirect ()->route ("post.show", ["post" =>$post]);
    }
    /**
     * Remove specified articles from memory
     *
     * @param int $id
     * @return response
     * /
    public function destroy ($id)
    {
      $post=post ::find ($id);
      if (! $post)
        exit ("Specified deleted article does not exist!");
      if ($post->delete ()) {
        redirect ()->route ("post.index");
      } else {
        exit ("Failed to delete article!");
      }
    }
  }

It should be noted that in the show method, we first fetch the article data from the cache.The database does not exist before fetching it.At the same time write the data back to the cache,Since most of the operations on the database are read operations,So this small improvement has greatly improved performance,Especially when it comes to massive data.In addition, we will persist the traffic to the cache to improve performance.

3.Use caching in model events

We can also use the model event to trigger the corresponding event when the article is added or deleted, and save the modification to the cache.Here we briefly talk about the model event registration in the boot method of appserviceprovider:

//Update cached data after saving
post ::saved (function ($post) {
  $cachekey="post _". $post->id;
  $cachedata=cache ::get ($cachekey);
  if (! $cachedata) {
    cache ::add ($cachekey, $post, 60 * 24 * 7);
  } else {
    cache ::put ($cachekey, $post, 60 * 24 * 7);
  }
});
//Clear cache data after deletion
post ::deleted (function ($post) {
  $cachekey="post _". $post->id;
  $cachedata=cache ::get ($cachekey);
  if ($cachedata) {
    cache ::forget ($cachekey);
  }
  if (cache ::get ("post_views _". $post->id))
    cache ::forget ("post_views _". $post->id);
});

We set the cache validity period to one week.This will save the data to the cache when the article is created or updated,Data is also removed from the cache when you delete an article.This ensures that deleted articles cannot be viewed when viewing details.

php
  • Previous node + experss to crawl movie heaven crawler
  • Next Very practical commands and common sense for Linux system operation and maintenance (super practical)