laravel redis cache将字符串长度添加到值

时间:2015-12-11 08:52:15

标签: laravel caching redis predis

我使用laravel / redis进行缓存,我意识到laravel在我的值中添加了一些字符串,数字是字符串长度。 这样的事: 当我将ABC字符串放入一个键时,重新获得的值是:

<?php

include_once('../include/config.php');
include_once('../include/functions.php');

getHeader('Boek updaten', 'Schrijf een boek!');

if (isset($_GET['action']) == 'save')
{
    $id = $_GET["id"];
    $boek_naam = $_POST['boek_naam'];
    $boek_text = $_POST['boek_text'];
    $boek_genre = $_POST['boek_genre'];


    $allow = array("jpg", "jpeg", "gif", "png");
    if(isset($_FILES['cover']['name'])) {
        $query = $db->prepare("
                    SELECT `boeken_cover` FROM `boeken` WHERE boeken_id = '$id'");
        $query->execute();
        foreach($query->fetchAll() as $row){
            $old_cover = $row['boeken_cover'];
        }
        chmod($old_cover, 0600);
        unlink($old_cover);
        $imgdir = "../images/";
        $fulldir = $imgdir.$_FILES['cover']['name'];
        if (!!$_FILES['cover']['tmp_name']) {
            $info = explode('.', strtolower($_FILES['cover']['name']));
            if (in_array(end($info), $allow)) {
                if (move_uploaded_file($_FILES['cover']['tmp_name'], $imgdir . basename($_FILES['cover']['name']))) {
                    echo "Foto uploaden gelukt";
                }
            }
        }
        try
        {
            $query=$db->prepare("
                    UPDATE `boeken` SET `boeken_naam` = :naam, `boeken_text` = :text, `boeken_genre` = :genre, `boeken_cover` = :fulldir WHERE `boeken_id` ='$id'");
            $query->bindParam(':naam', $boek_naam);
            $query->bindParam(':text', $boek_text);
            $query->bindParam(':genre', $boek_genre);
            $query->bindParam(':fulldir', $fulldir);
            $query->execute();
            echo "Het boek is geupdated. Klik <a href='index.php'>hier</a> om naar het overzicht te gaan.";
        }
        catch(PDOException $e)
        {
            $sMsg = '<p>
                Regelnummer: '.$e->getLine().'<br/>
                Bestand: '.$e->getFile(). '<br/>
                Foutmelding: '.$e->getMessage().'
                </p>';

            trigger_error($sMsg);
        }
    }
    else{
        try
        {
            $query=$db->prepare("
                    UPDATE `boeken` SET `boeken_naam` = '$boek_naam', `boeken_text` = '$boek_text', `boeken_genre` = '$boek_genre' WHERE `boeken_id` ='$id'");
            $query->execute();
            echo "Het boek is geupdated. Klik <a href='index.php'>hier</a> om naar het overzicht te gaan.";
        }
        catch(PDOException $e)
        {
            $sMsg = '<p>
                Regelnummer: '.$e->getLine().'<br/>
                Bestand: '.$e->getFile(). '<br/>
                Foutmelding: '.$e->getMessage().'
                </p>';

            trigger_error($sMsg);
        }
    }
}

else {
    $id = $_GET["id"];

    try {
        $query = $db->prepare("
                    SELECT * FROM `boeken` WHERE boeken_id = '$id'");

        $query->execute();
    } catch (PDOException $e) {
        $sMsg = "<p>
                Regelnummer: " . $e->getLine() . "<br />
                Bestand: " . $e->getFile() . "<br />
                Foutmelding: " . $e->getMessage() . "
                </p>";

        trigger_error($sMsg);
    }
    while ($rij = $query->fetch(PDO::FETCH_ASSOC)) {
        $boeken_id = $rij["boeken_id"];
        $boeken_naam = $rij["boeken_naam"];
        $boeken_genre = $rij["boeken_genre"];
        $boeken_text = $rij["boeken_text"];
    }

    echo "
        <form name = \"boeken updaten\" action=\"?action=save&id=$id\" method=\"post\" enctype=\"multipart/form-data\">
            <table>
                <tr>
                    <td>Boek naam</td>
                    <td><input type=\"text\" name=\"boek_naam\" value=\"$boeken_naam\" required> </td>
                </tr>
                <tr>
                    <td>Boek genre</td>
                    <td><input type=\"text\" name=\"boek_genre\" value=\"$boeken_genre\" required> </td>
                </tr>
                <tr>
                    <td>Cover</td>
                    <td><input type='file' name='cover' id='cover'></td>
                </tr>
                <tr>
                    <td>Inhoud</td>
                    <td><textarea cols='50' rows='20' name='boek_text'>$boeken_text</textarea></td>
                </tr>
                <tr>
                    <td colspan=\"2\" ><input type=\"reset\" name=\"reset\" value=\"Leeg maken\">
                    <input type=\"submit\" name=\"submit\" value=\"Updaten\"</td>
                </tr>
            </table>
        </form>";
}

getFooter();

?>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的问题在于Laravel缓存管理器序列化它存储的值,以便它可以存储更复杂的类型,如数组。

您要做的是unserialize值,有关详情,请参阅documentation

因此,通过您的预先调用,您可以执行反序列化的附加步骤

unserialize($predisClient->get('myKey'));

这样的价值将是&#34; ABC&#34;

更新

要考虑Vladislav Rastrusny comment如果您要存储不同类型的值,包括数字,那么您会想要做这样的事情

$value = $predisClient->get('myKey');
$result = is_numeric($value) ? $value : unserialize($value);

了解Laravel如何使用get方法here处理此问题。