0

I've been working with PHP (I'm very new to this) and I have this scenario:

I have a list with 3 (or more) IP addresses + ports that comes from a file, so each line of my file has:

192.168.3.41:8013
192.168.3.41:8023
192.168.3.41:8033

So in my array, these are elements array[0], array[1], array[2]. The purpose of the application is simple, ping the IP:PORT and know if it is responding and how many ping errors it has. But that is not all, I have to count the time it takes to do the process for 3 minutes and 1 minute. So, I've been asked to work with MemCache to do the following:

For each IP:PORT, I need to save in MemCache how many errors does I have in 3 minutes as well as the number of errors I have in 1 minute, so something like that:

For number of errors in 3 minutes

Map<Key, Value> = Map<IP:PORT, NumberOfErrors3Mins>

For number of errors in 1 minute

Map<Key, Value> = Map<IP:PORT, NumberOfErrors1Min>

So, a data sample could be like this:

For 3 minutes:

<192.168.3.41:8013, 5>
<192.168.3.41:8023, 2>
<192.168.3.41:8023, 0>

For 1 Minute:

<192.168.3.41:8013, 3>
<192.168.3.41:8023, 1>
<192.168.3.41:8023, 1>

So, I have two maps and 3 entries for each map. I'm pretty new to PHP and MemCache is kind of difficult to me, the logic I stablished is the following:

$array = array('192.168.3.41:8013','192.168.3.41:8023','192.168.3.41:8033');

for ($i = 0; $i < count($array); ++$i){
        $currentProxy = $array[$i];
        echo "working on $i : <br/>";
        echo "good to see you friend:".$currentProxy."<br/>";
        $res1 = $memt1->get($currentProxy);
        $res2 = $memt2->get($currentProxy);
        echo "response for proxy:".$res1."<br/>";
        echo "response for proxy:".$res2."<br/>";
        if (!$res1){
                echo "Does not exist - create<br/>";
                $memt1->set($currentProxy, 1, null, 0);
        } else {
                echo "Does exist - help<br/>";
                $memt1->increment($currentProxy);
        }

        if (!$res2){
                echo "Does not exist - create<br/>";
                $memt2->set($currentProxy, 1, null, 0);
        } else {
                echo "Does exist - help<br/>";
                $memt2->increment($currentProxy);
        }

}

The problem I am facing is that both memt1 and memt2 are referring to the same, for instance, if I increment <192.168.3.41:8013> for memt1, it also increment it for memt2, I think that this could be a very noob question, but I've just entered to PHP world and I don't know how to handle this at all, this is what I tried, if somebody could help me, please I would be really grateful. Thanks in advance.

1 Answer 1

0

I did it, it was simplier of what I thought before:

Basically, one MemCache for all the type of possible keys with values, I didn't know how to use it well, but now it works and fits perfect for me, hope it helps somebody who is starting with this:

$memHandler = new MemCache();
if ($memHandler) {
    $memHandler->connect('localhost','11211');
} else {
    die('Problem with MemCache');
}

$array1 = $memHandler->get('e1');
$array3 = $memHandler->get('e3');
$lcr1 = $memHandler->get('lcr1');
$lcr3 = $memHandler->get('lcr3');

if (!$array1) {
    echo 'Does not exist - create<br/>';
    $memHandler->set('e1', $newArray);
} else {
    echo 'Does exist<br/>';
    foreach ($array1 as $key => $value) {
        echo $key.':'.$value.'<br/>';
    }
}

if (!$array3) {
    echo 'Does not exist - create<br/>';
    $memHandler->set('e3', $newArray);
} else {
    echo 'Does exist<br/>';
     foreach ($array3 as $key => $value) {
             echo $key.':'.$value.'<br/>';
        }

}

echo '=============================<br/>';

if (!$lcr1){
    echo 'Does not exist - create date<br/>';
        $memHandler->set('lcr1', time());
} else {
    echo 'Does exist<br/>';
    echo $lcr1.'<br/>';
}

if (!$lcr3){ 
        echo 'Does not exist - create date<br/>';
        $memHandler->set('lcr3', time());
} else {
        echo 'Does exist<br/>';
        echo $lcr3.'<br/>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.